Javi Langunz
Javi Langunz

Reputation: 1

Does a class or struct have a default static constructor if not explicitly defined with one?

C# specification states that if one does not explicitly define a parameter-less constructor in a class or struct type, then the C# (I assume it's referring to the compiler, correct me if i'm wrong) provides one for them, so that when an instance of the type is created the default parameter-less constructor provided will be used to initialize instance fields to their default values if the instance fields are not intialized at declaration or by any parameterized constructor.

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 of C# types article. If you don't provide a constructor for your struct, C# relies on an implicit parameterless constructor to automatically initialize each field to its default value.

    public class Program
    {
        static void Main(string[] args)
        {
            //invoke default parameter-less constructor belonging to types MyClass and MyStruct
            MyClass inst1 = new MyClass();

            MyStruct inst2 = new MyStruct();
        }
    }

    public class MyClass
    {
        //body
    }

    public struct MyStruct
    {
        //body
    }

Yet, does the same go static constructors in a class type or struct type? In the sense that if you do not expliclty define a parameter-less static cosntructor, C# (I assume the compiler, correct me if i'm wrong) will provide one for you by default? In order to initialize static fields not initialized at declaration or at anypoint during program execution?

C# specification just states

Static constructors are parameterless. If you don't provide a static constructor to initialize static fields, the C# compiler initializes static fields to their default value as listed in the Default values of C# types article.

The above text doesn't answer the question though. Plus, I have no clue how to test this out in code (as I did with a default non-static parameter-less constructor) since a static constructor cannot be invoked explicitly in code.

Upvotes: -3

Views: 129

Answers (1)

Sweeper
Sweeper

Reputation: 273565

The presence of a static constructor actually matters (a little bit), as said here:

The presence of a static constructor prevents the addition of the BeforeFieldInit type attribute. This limits runtime optimization.

This implies that if you don't declare a static constructor, then there is no static constructor automatically generated. Because if there is always a static constructor generated for a class, then that sentence would not make much sense.

We can check by looking at the IL on SharpLab.

The extra static constructor is declared in IL as:

.method private hidebysig specialname rtspecialname static 
    void .cctor () cil managed 
{
    // Method begins at RVA 0x2050
    // Code size 1 (0x1)
    .maxstack 8

    IL_0000: ret
} // end of method C::.cctor

Upvotes: 2

Related Questions