tags:

views:

212

answers:

2

I believe (correct me if i am wrong), according to the C# rule for value types, there is no default constructor. The CLR will define the one for zeroing out the field values.

For reference type :

class Test
{

  private string Name;

}

Will the default constructor be supplied by C# or the CLR?

+11  A: 

In the CLI specification, a constructor is mandatory for non-static classes, so at least a default constructor will be generated by the compiler if you don't specify another constructor.

So the default constructor will be supplied by the C# Compiler for you.

Botz3000
Unless it's a static class, of course.
Jon Skeet
Of course. :)
Botz3000
+3  A: 

You will get the default constructor, so the result will be the same as you are writing. Although, the constructor itself won't have generated the instructions to zero your fields one by one (it will only call the base class constructor, here's one generated by compiler):

.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 
}

Although, before running the ctor, all bits of your class will be set to 0, so from your points of view, there's no difference.

Ravadre