Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why static constructor not called before first call to class method

Tags:

c#

.net

According to Jon Skeet's artice C# and beforefieldinit and discussion in When is a static constructor called in C#? static constructor must be called before first call to a method of the class.

For some reason following code does not exhibit this behavior:

namespace AbstractAndStatic
{
    class Program
    {
        static void Main(string[] args)
        {
            StaticClass.Equals(1,2);
            StaticClass.foo();
        }
    }
    static class StaticClass : Object
    {
        public static void foo()
        {
            Console.WriteLine("Static");
        }
         static StaticClass()
        {
            Console.WriteLine("static constructor");
        }
    }
    class TestClass
    {
        public void deb()
        {
            Console.WriteLine("Test Class Debug");
        }
    }
}     

I am debugging the above code using the Visual Studio Debugger. When the statement StaticClass.Equals(1,2); gets executed in the Main method the static Constructor is not getting called but when StaticClass.foo(); is executed it calls static constructor and then call the foo method.

I am little confused as why it didn't get called the the first time when executing StaticClass.Equals(1,2);.

like image 376
Sumit Rayakwar Avatar asked Sep 11 '15 14:09

Sumit Rayakwar


People also ask

Why is static constructor called first?

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

Why static is not used in constructor?

We know static keyword belongs to a class rather than the object of a class. A constructor is called when an object of a class is created, so no use of the static constructor.

Which constructor will be called first static or normal constructor?

Static constructors are used to initialize the static members of the class and are implicitly called before the creation of the first instance of the class. Non-static constructors are used to initialize the non-static members of the class.

When and how is static constructor called?

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


1 Answers

Your call to StaticClass.Equals is actually just a call to Object.Equals(Object, Object), as StaticClass doesn't provide an applicable overload for Equals. If you look in the IL, you'll see that the compiler has resolved the call to just Object.Equals(1, 2). If you're not calling a method which actually involves the static class, it doesn't need to be initialized.

like image 153
Jon Skeet Avatar answered Sep 17 '22 13:09

Jon Skeet