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

CSharp Static Constructor
C# Static Constructor

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

C# Static Constructor

A static constructor in C# is a special kind of constructor that is automatically called before the first instance of a class is created.
It is used to initialize only the static data members of a class, or to write code that needs to be executed only once during program execution.
The purpose of using a static constructor is to automatically trigger a piece of code without having to create a class instance.

C# Static Constructor Syntax

A static constructor syntax without any parameters or access modifiers.

 // Class
    public class Customer
    {
        // Static Constructor
      static Customer()
        {
            // Code
        }
    }
CSharp static class syntax
syntax: C# Static constructor

C# static constructor Characteristics:

Following are the characteristics of the static constructor.

      1. No parameters or access modifiers are accepted by a static constructor in C#.
      2. Static constructor never overloads, that’s why it doesn’t accept any input parameters.
      3. A class or struct can have only one static constructor.
      4. The static constructor gets executed only once for any number of objects created.
      5. The static constructor name must be the same as the class or struct name.
      6. A static constructor can’t have any  access modifiers because it doesn’t have any kind of scope.
      7. Static constructor does not have any return type not even  void .
      8. The static constructor gets invoked even before the  Main()  method in C#.
      9. It is independent of an object because it calls automatically by the  CLR , not by the developer.
      10. In C#, Non-static data members cannot be initialized in the static constructor.
      11. 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

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