Question about constructors of a class
-
Class a { a obj=new a(); } in this case we are calling the default constructor but class a { public a(int i) { console.writeline(i); } a obj=new a(); //Gives error during compilation } In this case we can't call default constructor after creating a custom constructor. But why it happns??? Why we cant use default constructor in this case
-
Class a { a obj=new a(); } in this case we are calling the default constructor but class a { public a(int i) { console.writeline(i); } a obj=new a(); //Gives error during compilation } In this case we can't call default constructor after creating a custom constructor. But why it happns??? Why we cant use default constructor in this case
Because it doesn't have one.
-
Because it doesn't have one.
means?, can u describe it
-
Class a { a obj=new a(); } in this case we are calling the default constructor but class a { public a(int i) { console.writeline(i); } a obj=new a(); //Gives error during compilation } In this case we can't call default constructor after creating a custom constructor. But why it happns??? Why we cant use default constructor in this case
In your first version of class a, you haven't specified a constructor. This means that a default constructor is created for you. As soon as you add a constructor, you lose the default constructor. This is exactly what should happen because you can't instantiate a class that doesn't have a constructor, and you don't want the compiler to give someone the ability to use a default constructor if your class relies on values that are passed in via the constructor.
I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier -
In your first version of class a, you haven't specified a constructor. This means that a default constructor is created for you. As soon as you add a constructor, you lose the default constructor. This is exactly what should happen because you can't instantiate a class that doesn't have a constructor, and you don't want the compiler to give someone the ability to use a default constructor if your class relies on values that are passed in via the constructor.
I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easierthnks for this answer i appreciate it
-
Class a { a obj=new a(); } in this case we are calling the default constructor but class a { public a(int i) { console.writeline(i); } a obj=new a(); //Gives error during compilation } In this case we can't call default constructor after creating a custom constructor. But why it happns??? Why we cant use default constructor in this case
Classes only have the constructors you create - if you don't create any at all then a parameterless constructor is created for you. Structs, being value types, are different and always have a default parameterless constructor with all the fields initialized to their default values (normally zero, or null if the field is a reference type).
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
Class a { a obj=new a(); } in this case we are calling the default constructor but class a { public a(int i) { console.writeline(i); } a obj=new a(); //Gives error during compilation } In this case we can't call default constructor after creating a custom constructor. But why it happns??? Why we cant use default constructor in this case
Because the language specification says so.
17.10.4 Default constructors
If a class contains no instance constructor declarations, a default instance constructor is automatically
provided. That default constructor simply invokes the parameterless constructor of the direct base class.Really it should be have said "if and only if", because that's what they mean. If you don't define a ctor, the default one exists. If you do define a ctor, the default one goes away. You can get it back if you want, because it's trivial:
public TypeName() { }
(it would be protected for abstract classes)