Static Constructor


In this article we are going to discuss about static constructor with examples in C#.

Static Constructor

A static constructor is used to initialize static members of the class. It is called only once, when the class is first loaded. If we declare a constructor as static, It will be called automatically before the first instance is created.

Features of Static Constructors

The below are the following features of Static Constructor.

Static Constructor in C#
  1. No Parameters - Static constructors do not take any parameters.

    Static Constructor in C#
  2. No Access Modifiers - They cannot have access modifiers (e.g., public, private, etc.).

    Static Constructor in C#

  3. Automatically Called - The runtime automatically calls the static constructor before the first instance is created or any static members are referenced.

    Static Constructor in C#
  4. Single Execution - A static constructor is called only once, and it’s called before any static member is accessed for the first time.

    Static Constructor in C#

    Output

    Static Constructor in C#

Syntax

Here is the syntax for defining a static constructor.

class MyClass
{
    static MyClass()
    {
        // Initialization code
    }
}

Example

The following code define the static constructor.

using System;

namespace delegateproject
{
    public class Person
    {
        // Static Constructor
        static Person()
        {
            Console.WriteLine("Called Static Constructor");
        }
    }
    class Program
    {
        static void Main()
        {
            // Usage
            Person person = new Person();
            Console.ReadLine();
        }
    }
}

Output

Static Constructor in C#

Static Constructor - Order of Execution

The static constructor is executed before any static member is accessed or any instances of the class are created.

Example

using System;

namespace delegateproject
{
    public class Person
    {
        // Instance constructor
        public Person()
        {
            Console.WriteLine("Instance constructor called.");
        }
        // Static Constructor
        static Person()
        {
            Console.WriteLine("Called Static Constructor");
        }
    }
    class Program
    {
        static void Main()
        {
            // Usage
            Person person = new Person();
            Console.ReadLine();
        }
    }
}

Output

Static Constructor in C#

Static Constructor - Access the Static Member

Example

Consider the following example that demonstrates a static constructor in use:

using System;

namespace delegateproject
{
    public class Person
    {
        public static int StaticField;
        // Instance constructor
        public Person()
        {
            Console.WriteLine("Instance constructor called.");
        }
        // Static Constructor
        static Person()
        {
            StaticField = 10;
            Console.WriteLine("Called Static Constructor");
        }
        public static void Display()
        {
            Console.WriteLine($"StaticField: {StaticField}");
        }
    }
    class Program
    {
        static void Main()
        {
            // Accessing the static member for the first time
            Person.Display();
            
            Person person = new Person();
            Console.ReadLine();
        }
    }
}

Output

Static Constructor in C#

When to Use Static Constructors

  • When you need to initialize static data that is not straightforward or requires more logic than simple assignments.
  • When you want to perform some initialization tasks related to the type itself, rather than its instances.

Where Use Static constructor

Static constructors are used for:

  1. Initializing Static Fields - When you need to initialize static fields or properties that require complex setup.
  2. Enforcing Initialization Logic - When there are certain actions that need to be performed only once, such as logging, setting up resources, etc.

Prev Next