C# Static Constructor-The top 10 points you should know

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

CSharp Static Constructor
C# Static Constructor

C# Static Constructor

In C#, a static constructor is a type of constructor that is invoked automatically before a class’s first instance is created.
It’s used to just initialize static data members of a class or to write code that only has to run once during the execution of a program.
The objective of a static constructor is to automatically execute code without the need to create a class instance.

C# Static Constructor Syntax

c# static constructor syntax
C# Static constructor

The examples below show how to declare static constructor syntax with no parameters or access modifiers.

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

C# static constructor Characteristics:

The static constructor has the following characteristics.

  • No parameters or access modifiers are accepted by a static constructor in C#.
  • The static constructor never overloads, that’s why it doesn’t accept any input parameters.
  • A class or struct can have only one static constructor.
  • The static constructor gets executed only once for any number of objects created.
  • The static constructor name must be the same as the class or struct name.
  • A static constructor can’t have any  access modifiers because it doesn’t have any kind of scope.
  • Static constructor does not have any return type not even  void .
  • 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#, Non-static data members cannot be initialized in the static constructor.
  • A static constructor is useful when we want to create utility classes ( Ex. Math, Console, Convert, etc.), or a log file to write the entries.

Example of static constructor in C#

Following is the example of a static constructor in C#

using System;
namespace StaticConstructorExample
{  
    // Customer 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 !

     */
}

The output of static constructor program in C#
The output of the above C# static constructor 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.

Note: If you don’t provide a static constructor to initialize static fields or variables, the C# compiler initializes static fields to their default value.

References MSDN: Constructors

As per MSDN: 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.

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 below.

Articles to Check Out

Leave a Comment