Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which static class initializes first?

Which static class initializes first if we have one more static classes in our project?

For example: Below code gives null exception.

class Program
    {
        static void Main(string[] args)
        {
            First.Write();
            Second.Write();
        }
    }
    static class First
    {
        public static int[] firstArray = new int[20];
        public static int[] secondArray = Second.secondArray;
        public static void Write()
        {
            Console.WriteLine(firstArray.ToString());
            Console.WriteLine(secondArray.ToString());
        }
    }
    static class Second
    {
        public static int[] firstArray = First.firstArray;
        public static int[] secondArray = new int[30];
        public static void Write()
        {
            Console.WriteLine(firstArray.ToString());
            Console.WriteLine(secondArray.ToString());
        }
    }

If you pay attention, you will see that if First class will initialize itself so secondArray field of Second would be null. But if Second class would initialize first so Second class firstArray would be null. I am trying to tell that which initialize first makes different results.

I think that it is abstract question about my project. I encounter it while trying to understand why I am getting unexpected results.

like image 487
Freshblood Avatar asked Jul 01 '10 11:07

Freshblood


People also ask

Are static variables initialized first?

It is a variable which belongs to the class and not to object(instance ). Static variables are initialized only once, at the start of the execution. These variables will be initialized first, before the initialization of any instance variables.

Are static variables initialized before constructor?

All static members are always initialized before any class constructor is being called.

Can we initialize static class?

Static classes cannot be instantiated. All the members of a static class must be static; otherwise the compiler will give an error. A static class can contain static variables, static methods, static properties, static operators, static events, and static constructors.

Is initialization necessary for static variable?

Static variables are initialized only once. Compiler persist the variable till the end of the program. Static variable can be defined inside or outside the function. They are local to the block.


1 Answers

First will begin to initialize, assign firstArray, then notice that it requires Second to be initialized in order to get the initial value of secondArray.

Second will start initializing, and then notice that it requires First to be initialized. However, the CLR will then notice that First is already initializing in the current thread, so it won't block. Second's initialization will complete, and then First's initialization will complete.

Fortunately, the field that Second needs has already been assigned, so the "right thing" happens.

That's all very well if First actually starts initializing first. However, as neither of the classes has a static constructor, it's possible that Second will start to initialize first... it would then start to initialize First, which would spot that Second is already initializing and take Second.secondArray's current value (null) for First.secondArray. This would be a Bad Thing. Note that the initialization timing for types without static constructors has changed in .NET 4 - not in a spec-breaking way, but possibly in an existing-code-breaking way.

If both First and Second had static constructors, then First would be initialized first, as that's the first class that Main touches.

Moral of the answer: don't do this. Type initializers which refer to each other are very error prone. For another example, see Eric Lippert and Neal Gafter's NDC 2010 talk, "C# Puzzlers" which can be viewed on the NDC video page.

like image 192
Jon Skeet Avatar answered Sep 19 '22 16:09

Jon Skeet