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
What is a static constructor in C#?
A static constructor is a special constructor declared using the static modifier. It is the first block of code executed when a class is accessed for the first time. A static constructor executes only once during the entire lifetime of the application, regardless of how many instances of the class are created.
Static constructors are primarily used to initialize static fields or perform one-time setup operations for a class.
Syntax
Following is the syntax for declaring a static constructor −
static ClassName() {
// initialization code
}
Key Rules of Static Constructors
-
A static constructor cannot have access modifiers (public, private, etc.).
-
A static constructor cannot take parameters.
-
A static constructor is called automatically ? you cannot call it explicitly.
-
It executes only once per application domain.
-
It runs before any instance constructor or static member is accessed.
Using Static Constructor for Initialization
Example
using System;
class Demo {
static int val1;
int val2;
static Demo() {
Console.WriteLine("This is Static Constructor");
val1 = 70;
}
public Demo(int val3) {
Console.WriteLine("This is Instance Constructor");
val2 = val3;
}
public void Show() {
Console.WriteLine("First Value = " + val1);
Console.WriteLine("Second Value = " + val2);
}
static void Main(string[] args) {
Demo d1 = new Demo(110);
Demo d2 = new Demo(200);
d1.Show();
d2.Show();
}
}
The output of the above code is −
This is Static Constructor This is Instance Constructor This is Instance Constructor First Value = 70 Second Value = 110 First Value = 70 Second Value = 200
Static Constructor with Static Fields
Example
using System;
class Counter {
private static int totalCount;
private static readonly string applicationName;
static Counter() {
totalCount = 0;
applicationName = "Counter Application";
Console.WriteLine("Static constructor called. App initialized: " + applicationName);
}
public Counter() {
totalCount++;
Console.WriteLine("Instance created. Total count: " + totalCount);
}
public static void DisplayTotal() {
Console.WriteLine("Total instances created: " + totalCount);
}
}
class Program {
public static void Main() {
Counter c1 = new Counter();
Counter c2 = new Counter();
Counter.DisplayTotal();
}
}
The output of the above code is −
Static constructor called. App initialized: Counter Application Instance created. Total count: 1 Instance created. Total count: 2 Total instances created: 2
Static Constructor vs Instance Constructor
| Static Constructor | Instance Constructor |
|---|---|
Declared with static keyword |
Declared without static keyword |
| Executes only once per class | Executes each time an object is created |
| Cannot have parameters | Can have parameters |
| Cannot have access modifiers | Can have access modifiers (public, private, etc.) |
| Initializes static members | Initializes instance members |
Conclusion
Static constructors in C# execute once when a class is first accessed and are used to initialize static fields or perform one-time setup. They run automatically before any instance constructors and cannot be called explicitly or take parameters.
