-3

In general the compiler adds the default constructor when we dont declare any constructor explicitly. In the same way does it also allocate a static constructor by default. If no, why? If yes, why? and in which situation?

9
  • C# compliler does not add the static constructor in any class, it adds the instance constructor by default when you do not specify any. It adds so that you can create instance of your class.
    – Gaurav
    Jan 9, 2019 at 5:37
  • Conceptually, you should think of it as not adding it. Why would it need to? For a class it needs to so you can instantiate it. But you never explicitly call a static constructor, so whether it does or doesn't add an empty constructor is an implementation detail that has no meaningful impact on you.
    – mjwills
    Jan 9, 2019 at 5:37
  • 1
    Why you think that ? @CodeMan In the absence of an explicitly defined instance constructor, one will be automatically created 'behind the scenes'. This is what the OP means. See 10.11.4 of the C# language spec.
    – mjwills
    Jan 9, 2019 at 5:44
  • 1
    @AkarshaRao, as you can see, when you are not declaring static constructor for YourType explicitly, typeof(YourType).TypeInitializer is null, so I think there is no autogenerated static constructor.
    – vasily.sib
    Jan 9, 2019 at 6:18
  • 1
    I'll post an answer to summarize all my knowledge
    – vasily.sib
    Jan 9, 2019 at 6:33

4 Answers 4

4

If you don’t provide a constructor for your class, C# creates one by default that instantiates the object and sets member variables to the default values as listed in the Default Values Table. Constructor without any parameters is called a default constructor. In other words, this type of constructor does not take parameters. The drawback of a default constructor is that every instance of the class will be initialized to the same values and it is not possible to initialize each instance of the class to different values.

The default constructor initializes:

All numeric fields in the class to zero. All string and object fields to null.

2
3

Yes. Only in one case: When a class or structure has a static variable instialised to custom defaults(say static int age=18;).In this particular case, a default static constructor is added to the IL code. Apart from this case, there will be no scope of default static constructor.

1

No, there is no automatically generated static constructors in C#.

There is no need for compiler to create automatic static constructor - the only items that may need to be initialized in static constructor are static fields/properties but that initialization happens outside of the static constructor. This is not the case for instance constructor because you need a constructor to be able to create an instance (unless it is static class) - so compiler had to create one if there is none specified.

Static field initialzation:

...If a static constructor exists in the class, execution of the static field initializers occurs immediately prior to executing that static constructor. Otherwise, the static field initializers are executed at an implementation-dependent time prior to the first use of a static field of that class.

More information can be found for example in Classes section of C# 6.0 draft specification. In particular you can see that Default constructors explicitly calls out automatically provided one:

If a class contains no instance constructor declarations, a default instance constructor is automatically provided.

while Static constructors does not have any information about it being "automatically provided".

-1

You and @mjwills are right. In the absence of an explicitly defined instance constructor, one will be automatically created by CLR see 10.11.4 of the C# language spec., so this two classes are equal (except the name of course) in IL:

class A1 { }
class A2 { public A2() : base() { } }

You can check if there any static constructor within the class like this: typeof(A1).TypeInitializer, or more precisely typeof(A1).GetConstructors(BindingFlags.Static | BindingFlags.NonPublic).

So, lets do some easy checking:

class A { }
typeof(A).TypeInitializer == null // true
typeof(A).GetConstructors(BindingFlags.Static | BindingFlags.NonPublic).Length == 0 // true

class B { static B { } }
typeof(B).TypeInitializer != null // true
typeof(B).GetConstructors(BindingFlags.Static | BindingFlags.NonPublic).Length == 1 // true

However, if you use some static field or property initializers, CLR will add some "default" static constructor for you:

class C { public static int I = 1; }
typeof(B).TypeInitializer != null // true
typeof(B).GetConstructors(BindingFlags.Static | BindingFlags.NonPublic).Length == 1 // true

class D { public static int P { get; set; } = 1; }
typeof(B).TypeInitializer != null // true
typeof(B).GetConstructors(BindingFlags.Static | BindingFlags.NonPublic).Length == 1 // true

One more check without field and property initializers:

class E { public static int I; public static int P { get; set; } }
typeof(E).TypeInitializer == null // true
typeof(E).GetConstructors(BindingFlags.Static | BindingFlags.NonPublic).Length == 0 // true
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.