Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to call a static constructor or when static constructor is called in C#?
A static constructor in C# is called automatically by the Common Language Runtime (CLR) before the first instance of a class is created or any static members are referenced. It is used to initialize static data or perform actions that need to be executed only once during the application's lifetime.
Unlike instance constructors, static constructors cannot be called directly and have no control over when they execute − the CLR handles their invocation automatically.
Syntax
Following is the syntax for declaring a static constructor −
static ClassName() {
// initialization code
}
Key Properties of Static Constructors
-
A static constructor does not take access modifiers or have parameters.
-
A class or struct can only have one static constructor.
-
Static constructors cannot be inherited or overloaded.
-
A static constructor cannot be called directly and is only meant to be called by the common language runtime (CLR). It is invoked automatically.
-
The user has no control over when the static constructor is executed in the program.
When Static Constructor is Called
The static constructor is automatically invoked in the following scenarios −
-
Before the first instance of the class is created
-
Before any static members are accessed
-
Before any static methods are called
Example - Static Constructor with Instance Creation
using System;
class MyClass {
public static string staticMessage;
static MyClass() {
staticMessage = "Static constructor called!";
Console.WriteLine("Static constructor executed");
}
public MyClass() {
Console.WriteLine("Instance constructor executed");
}
}
class Program {
public static void Main() {
Console.WriteLine("Creating first instance...");
MyClass obj1 = new MyClass();
Console.WriteLine("Creating second instance...");
MyClass obj2 = new MyClass();
Console.WriteLine("Static message: " + MyClass.staticMessage);
}
}
The output of the above code is −
Creating first instance... Static constructor executed Instance constructor executed Creating second instance... Instance constructor executed Static message: Static constructor called!
Using Static Constructor for Static Member Access
Example - Static Constructor Triggered by Static Member
using System;
class Counter {
public static int count;
static Counter() {
count = 100;
Console.WriteLine("Static constructor: Counter initialized to " + count);
}
public static void Increment() {
count++;
Console.WriteLine("Count incremented to: " + count);
}
}
class Program {
public static void Main() {
Console.WriteLine("Accessing static member...");
Console.WriteLine("Initial count: " + Counter.count);
Console.WriteLine("Calling static method...");
Counter.Increment();
Counter.Increment();
}
}
The output of the above code is −
Accessing static member... Static constructor: Counter initialized to 100 Initial count: 100 Calling static method... Count incremented to: 101 Count incremented to: 102
Static Constructor vs Instance Constructor
| Static Constructor | Instance Constructor |
|---|---|
| Called once per class, automatically by CLR | Called every time an instance is created |
| No access modifiers allowed | Can have access modifiers (public, private, etc.) |
| Cannot have parameters | Can have parameters |
| Used to initialize static members | Used to initialize instance members |
| Cannot be called directly | Called explicitly with 'new' keyword |
Conclusion
Static constructors in C# are automatically called by the CLR before any static members are accessed or instances are created. They execute only once per application domain and are primarily used to initialize static data, making them essential for one-time setup operations in your classes.
