In this article, we’ll explain about Static Constructor in C# with Examples. This article will be useful to both beginners and professional C# developers. We hope at the end of this article, all your doubts regarding Static Constructors in C# will be cleared.

What is default Constructor?

Before moving towards Static Constructors, let’s check default Constructor first. Every class is having a default constructor or explicitly we can create constructor. This default constructor is public (by default), We can also create a “static constructor” as per our needs. How the Static constructor appears, let’s see below:

Static Constructor in C#: Syntax

Static Constructor in C#

What is Static Constructor in C#?

A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.

Overall, C# supports multiple types (4 types) of constructors, here we’ll discuss only static constructor. Static constructor is used to initialize static data members as soon as the class is referenced the first time. Instance constructor is used to create an instance of that class with the ‘new’ keyword.

Important points about Static Constructor in C#

  • C# static constructor cannot have any access modifier like public/private etc.
  • C# static constructor cannot have any input parameter.
  • C# static constructor is invoked implicitly. It can’t be called explicitly.
  • The static constructor for a class executes before any instance of the class is created.
  • Static constructor for a class executes before any of the static members for the class are executed.
  • Static constructor for a class executes at most one time in whole lifetime of program.
  • The user cannot call ’static constructor’ directly and has no control on it when it is executed.
  • A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.

Code sample of Static Constructor:

using System;
namespace TestApplication{
   public class Program{
      static void Main(string[] args){
         Bank obj = new Bank ();
         //Bank obj1 = new Bank ();
         Console.ReadLine();
      }
   }
   public class Bank{
      static Bank()
       {
         Console.WriteLine("Static constructor is called");
       }
      public Bank ()
       {
         Console.WriteLine("Default constructor is called");
      }
   }
}

Out will be:

Static constructor is called.
Default constructor is called
Default constructor is called

Can we initialize non-static data fields within static constructor?

It is not possible to initialize non-static data fields within the static constructor, it will give compile time error. But we can initialize static data fields within a non-static constructor.

What happens if static constructor throws an exception?

If a static constructor throws an exception, the runtime will not invoke it a second time, and the static data members will remain uninitialized for the lifetime of the application. Error will be as

Static Constructor in C#

Can we use access modifiers & input parameter in static constructor?

No, we cannot pass input parameter and defining access modifier is also not feasible in static constructor. This in not feasible because ‘static constructor’ is the first piece of code that execute when that class is referred first time.

In this article I tried to explain Static Constructors in C# with Examples. I hope you enjoyed reading this article. For more information you can check out Microsoft learn.

Should read below article and see Video too:

For more information you can visit our C# Section for more such articles.

Explore More

Working With File Class In C#

In C#, ‘File’ class provides many static methods for moving, copying, and deleting files. These Static methods involve moving a file and copying and deleting a file also. Let’s check

How to Read a Text File in C#

In C# ‘File’ class provides static methods to read given text file data. The File.ReadAllText() method opens a text file, reads all the text present in the file into a

C# | How to use Interface references

In C#, you’re permitted to make a reference variable of an interface type or in other words, you’re permitted to form an interface reference variable. Such kind of variable can