Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone!

Every class will have a constructor.If programmer defines that contructor then it will have a body.

But for default constructor which was not defined by a programmer can it have a body?
Posted
Updated 23-Aug-10 22:31pm
v2
Comments
Dalek Dave 24-Aug-10 4:32am    
Minor Edit for Readability.

Even generated from compiler the default constructor has a body.
Here is an example for a default constructor generated by the compiler (I've used ildasm[^] to get the IL code):
MSIL
.method public hidebysig specialname rtspecialname instance void .ctor() cil managed
{
    .maxstack 8
    L_0000: ldarg.0
    L_0001: call instance void [mscorlib]System.Object::.ctor()
    L_0006: ret
}

From the IL code above you can easily spot the call to the default constructor of the base class (in our case System.Object).
Additionally if your class is static no constructors will be generated.

:)
 
Share this answer
 
v2
Comments
senguptaamlan 24-Aug-10 5:44am    
Reason for my vote of 1
good investigation..and good explanation
Nuri Ismail 24-Aug-10 6:08am    
If investigation and explanations are good, then why you vote 1?
You know that 1 and 2 are down-votes and 4 and 5 are up-votes, right?
hi laxmi_hemu,

I am not sure I fully understand the question, but in C# if you don't define a default constructor a default constructor will be implicitly defined for you in the class. It initialises the member variables to their default values.

So even if you have a class without a constructor defined, you can still fo something like

MyClass myClass = new MyClass();


This link from MSDN should give you more insight. MSDN: Constructors[^]

Hope this helps :)
Dave
 
Share this answer
 
Comments
Nuri Ismail 24-Aug-10 3:51am    
"in C# if you don't define a default constructor a default constructor will be implicitly defined for you in the class" - This is valid only for non-static classes. :)
DavidKiryazi 24-Aug-10 3:57am    
thanks for further info :) also sorry for double answer ;)
Nuri Ismail 24-Aug-10 4:05am    
No problem man! :)
The constructor which takes no argument is called default constructor.And it wil contains some coding part in it, which depends on the requirement of the application.
 
Share this answer
 
Comments
Toli Cuturicu 24-Aug-10 6:39am    
Reason for my vote of 1
non-sense
Sandeep Mewara 24-Aug-10 12:51pm    
Reason for my vote of 1
Was this for implicit default constructor?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900