C# | Difference between Static Constructors and Non-Static Constructors

Prerequisite: Constructors in C#

Static constructors are used to initializing the static members of the class and implicitly called before the creation of the first instance of the class. Non-static constructors are used to initializing the non-static members of the class. Below are the differences between the Static Constructors and Non-Static Constructors.

  • Declaration: Static constructors are declared using a static modifier explicitly while all other remaining constructors are non-static constructors. Non-static constructors can also be called as Instance Constructors as they need instance to get executed.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# Program to demonstrate
    // how to declare the static
    // constructor and non-static
    // constructor
    using System;
      
    class Geeks {
        // static variable
        static int s;
      
        // non-static variable
        int ns;
      
        // declaration of
        // static constructor
        static Geeks()
        {
            Console.WriteLine("It is static constructor");
        }
      
        // declaration of
        // non-static constructor
        Geeks()
        {
            Console.WriteLine("It is non-static constructor");
        }
      
        // Main Method
        static void Main(string[] args)
        {
      
            // static constructor will call implicitly
            // as soon as the class start to execute
            // the first block of code to execute
            // will be static constructor
      
            // calling non-static constructor
            Geeks obj1 = new Geeks();
        }
    }

    chevron_right

    
    

    Output:

    It is static constructor
    It is non-static constructor
    
  • Calling: Static constructors are always called implicitly but the non-static constructors are called explicitly i.e by creating the instance of the class.

    Example: In the above program, we have static constructor i.e static Geeks() which is called in the main method implicitly. See the ouput carefully, the code inside the static constructor is executing. But to call non-static constructor i.e Geeks(), you need to create an instance of the class, i.e obj1. So the creation of the object is to call the non-static constructor explicitly.

  • Execution: Static constructor executes as soon as the execution of a class starts and it is the first block of code which runs under a class. But the non-static constructors executes only after the creation of the instance of the class. Each and every time the instance of the class is created, it will call the non-static constructor.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# Program to demonstrate
    // the execution of static
    // constructor and non-static
    // constructor
    using System;
      
    class Geeks {
      
        // declaration of
        // static constructor
        static Geeks()
        {
            Console.WriteLine("Static constructor");
        }
      
        // declaration of
        // non-static constructor
        Geeks()
        {
            Console.WriteLine("Non-Static constructor");
        }
      
        // Main Method
        static void Main(string[] args)
        {
      
            // static constructor will call implicitly
            // as soon as the class start to execute
            // the first block of code to execute
            // inside the class will be static
            // constructor
      
            // calling non-static constructor
            // here we are calling non-static
            // constructor twice as we are
            // creating two objects
            Geeks obj1 = new Geeks();
            Geeks obj2 = new Geeks();
        }
    }

    chevron_right

    
    

    Output:



    Static constructor
    Non-Static constructor
    Non-Static constructor
    

    Explanation: In the above program, there are two objects of Geeks() class is created i.e obj1 and obj2. obj1 and obj2 will call the non-static constructor twice because each and every time the instance of the class is created, it will call the non-static constructor. Although, in the Main method(entry point of a program), the first statement is “Geeks obj1 = new Geeks();” but as soon as the compiler found the Main Method control will transfer to class and the static block will be executed first. That’s why you can see in the output that Static Constructor is called first.

  • Times of Execution: A static constructor will always execute once in the entire life cycle of a class. But a non-static constructor can execute zero time if no instance of the class is created and n times if the n instances are created.

    Example: In the above program, you can see the static constructor is executing only once but the non-static constructor is executing 2 times as the two instances of the class is created. If you will not create an instance of the class then the non-static constructor will not execute.

  • Initialization of fields: Static constructors are used to initialize the static fields and non-static constructors are used to initialize the non-static fields.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# Program to demonstrate
    // initialization of fields
    // by using static constructor
    // and non-static constructor
    using System;
      
    class Geeks {
      
        // static variable
        static int s;
      
        // non-static variable
        int ns;
      
        // declaration of
        // static constructor
        static Geeks()
        {
            Console.WriteLine("Static constructor");
        }
      
        // declaration of
        // non-static constructor
        Geeks()
        {
            Console.WriteLine("Non-Static constructor");
        }
      
        // Main Method
        static void Main(string[] args)
        {
      
            // static fields can
            // be accessed directly
            Console.WriteLine("Value of s is: " + s);
      
            // calling non-static constructor
            Geeks obj1 = new Geeks();
      
            // printing the value
            // of non-static field
            Console.WriteLine("Value of ns is: " + obj1.ns);
        }
    }

    chevron_right

    
    

    Output:

    Static constructor
    Value of s is: 0
    Non-Static constructor
    Value of ns is: 0
    

    Explanation: Here, both the static and non-static fields are initialized with default value. The default value of int type is zero. Static constructor will initialize only static fields. Here static field is s. While the non-static field(ns) is initialized by the non-static constructor.

  • Parameters: We cannot pass any parameters to the static constructors because these are called implicitly and for passing parameters, we have to call it explicitly which is not possible. It will give runtime error as shown in below example. However, we can pass the parameters to the non-static constructors.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# Program to demonstrate
    // the passing of paramters
    // to constructor
    using System;
      
    class Geeks {
      
        // static variable
        static int s;
      
        // non-static variable
        int ns;
      
        // declaration of
        // static constructor
        // and passing parameter
        // to static constructor
        static Geeks(int k)
        {
      
            k = s;
            Console.WriteLine("Static constructor & K = " + k);
        }
      
        // declaration of
        // non-static constructor
        Geeks()
        {
            Console.WriteLine("Non-Static constructor");
        }
      
        // Main Method
        static void Main(string[] args)
        {
        }
    }

    chevron_right

    
    

    Runtime Error:

    prog.cs(18, 16): error CS0132: `Geeks.Geeks(int)’: The static constructor must be parameterless

  • Overloading: Non-static constructors can be overloaded but not the static constructors. Overloading is done on the parameters criteria. So if you cannot pass the parameters to the Static constructors then we can’t overload it.
  • Cases in which the constructor will be implicit: Every class except the static class(which contains only static members) always contains an implicit constructor if the user is not defining any explicit constructor. If the class contains any static fields then the static constructors are defined implicitly.


My Personal Notes arrow_drop_up

Believes in Smart Work

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.