Static Constructor in C# with Examples

Description:

In the previous post, We have discussed about the Default constructor, Parameterized constructor, and Copy constructor.

Now in this post, We will learn about the static constructor with examples in C#.

C# Static Constructor

A  static  constructor is a special kind of constructor that invokes automatically before the first instance of a class is created.
It is used to initialize only static data members of the class or to write the code that needs to be executed only once throughout the execution of the program.

The purpose of using a static constructor is to trigger a piece of code automatically without creating an object of a class.

Static Constructor Syntax

In the below sample program, a static constructor is created without any parameter and access modifier.

 // Class
    public class Customer
    {
        // Static Constructor
      static Customer()
        {
            // Code
        }
    }

Points to Remember about static constructor

      • In C#, A class or struct can have only one static constructor.
      • The static constructor name must be the same as the class or struct name.
      • Static constructor never overloads that’s why it doesn’t accept any parameters.
      • A static constructor can’t have any  access modifiers because it doesn’t have any kind of scope.
      • It does not have any return type not even  void .
      • The static constructor gets executed only once for any number of object creations.
      • The static constructor gets invoked even before the  Main()  method in C#.
      • It is independent of an object because it calls automatically by the  CLR , not by the developer.
      • In C#, It is not possible to initialize non-static data members within the static constructor.
      • Static constructor is useful when we want to create a log file to write the entries.

Example of static constructor in C

Following are the example of static constructor in C#

using System;
namespace StaticConstructor
{  
    // Class
    public class Customer
    {
        // Static Constructor

      static  Customer()
        {
            Console.WriteLine("Static constructor executed !");         
        }

        //Instance or Default constructor

       public Customer()
        {
            Console.WriteLine("Default constructor executed !");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {

    // Here Static and instance constructors
   // will invoke for the first instance
            Customer customer1 = new Customer();

  // Here only only default constructor will invoke
 // not the static constructor.
            Customer customer2 = new Customer();

            Console.ReadLine();
        } 
    }
    /* Output:
     Static constructor executed !
     Default constructor executed !
     Default constructor executed !

     */
}

Output of static program in C#
The output of the above C# program.

In the above example, the Static constructor gets invoked only once at the time of the first object creation of the “Customer” class.
For the next object creation, only the default constructor will be called.

What is the difference between Private and Static Constructor?

Following are the difference between the static and private constructor.

Private Constructor:

      • In C#, Private Constructor is used to preventing any external instantiation of a class while allowing internal instantiation, which is something static constructor doesn’t allow.
      • A private constructor is an instance constructor called after the instance of the class is created, Whereas A static constructor is called before the first instance of a class is created.
      • A class that has a private constructor is not allowed to be inherited by any external class.
        Private constructors can be useful when creating a Singleton class.

Static Constructor:

      • A static constructor is used to initialize static data members of a class.        
      • A class can have only one static constructor.            
      • A static constructor is a special kind of method which doesn’t have any access modifiers.                         
      • A Static constructor can’t be overloaded because it doesn’t accept any parameters.
      • In C#, the Static constructor can not be invoked explicitly by the developer.
      • The static constructor gets executed only once for any number of object creations of a class.

Summary:

In this article, we learned about the static constructor in C# with examples. I hope you enjoyed this post and found it useful. In case you have any doubt, please post your feedback, question, or comments.

Please let me know if you like this article.

Thanks for visiting 🙂

Leave a Reply