3

I am wondering why my static constructor is outputting default constructor Static Constructor, and not the other way around Static Constructor and Default constructor or just Default constructor. When I use a static constructor, it should execute the static constructor first. However, from the code below,

The First question: why is the default constructor is called before the static constructor?

class Program
{
    static void Main(string[] args)
    {
        var test = Single.S;
    }
    class Single{
        static readonly Single s = new Single();

        public static Single S{
            get { return s; }
        }
        private Single(){
            Console.WriteLine("Default");

        }
        static Single(){
            Console.WriteLine("staic");
        }
    }
}

The Second question: How come the Static Single constructor is being called as well?

Anas Alweish
  • 2,818
  • 4
  • 30
  • 44
Shadman Mahmood
  • 133
  • 1
  • 3
  • 15
  • If you were to initialize an instance class, the members would be initialized first, and then the constructor would be run. Since you've added a static constructor, I imagine the same thing is happening. I think [this](https://stackoverflow.com/questions/42753909/static-constructor-is-called-before-any-static-members-are-referenced) is the same. – ProgrammingLlama Oct 27 '18 at 03:11
  • Possible duplicate of [Static constructor is called before any static members are referenced](https://stackoverflow.com/questions/42753909/static-constructor-is-called-before-any-static-members-are-referenced) – ProgrammingLlama Oct 27 '18 at 03:12

2 Answers2

3

Depending on Microsoft

A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.

class SimpleClass
{
    // Static variable that must be initialized at run time.
    static readonly long baseline;

    // Static constructor is called at most one time, before any
    // instance constructor is invoked or member is accessed.
    static SimpleClass()
    {
        baseline = DateTime.Now.Ticks;
    }
}

In this case, the static constructor will be called before the default constructor


but in this line,

static readonly Single s = new Single();

you are using "static field initialization"

The static field variable initializers of a class correspond to a sequence of assignments that are executed in the textual order in which they appear in the class declaration. If a static constructor exists in the class, execution of the static field initializers occurs immediately prior to executing that static constructor.

in this case, static field initialization will be completed before the constructor is called,

then the static constructor is run before your default constructor,

but when it runs, it's using new Single(), so passing through your non-static constructor path.

This causes to call the default constructor before the static constructor.

Finally, you can try this and you will see it will execute the static constructor before the default constructor

private class Single
{
    private static readonly Single s;
    static Single()
    {
        Console.WriteLine("static");
        s = new Single();
    }
}

References

Anas Alweish
  • 2,818
  • 4
  • 30
  • 44
0

The first thing that happens here when loading the class is

static Single s = new Single();

C# executes static initializers like this in the order they are seen. I am not sure if this ordering point also applies to the static constructor but from your test it seems that the static initializer does occur before the static constructor.

In any case, in order to assign to static Single s, one must first construct new Single(), which means invoking the non-static constructor. I am not sure what you would get if you read from Single.s in the non-static constructor but I would expect either null or an exception.

sjb-sjb
  • 1,112
  • 6
  • 14