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
Difference between Static Constructor and Instance Constructor in C#
In C#, constructors are special methods used to initialize objects. There are two main types: static constructors and instance constructors. Understanding their differences is crucial for proper class initialization and memory management.
Static Constructor
A static constructor is declared using the static modifier and is responsible for initializing static members of a class. It executes only once during the application lifetime, before any static members are accessed or any instances are created.
Syntax
static ClassName() {
// initialize static members
}
Instance Constructor
An instance constructor initializes instance data when an object of the class is created. It runs every time a new instance is instantiated and can accept parameters for customized initialization.
Syntax
public ClassName() {
// initialize instance members
}
public ClassName(parameters) {
// parameterized constructor
}
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
Key Differences
| Aspect | Static Constructor | Instance Constructor |
|---|---|---|
| Access Modifier | Cannot have access modifiers | Can be public, private, protected, etc. |
| Parameters | Cannot have parameters | Can have parameters |
| Execution | Once per class lifetime | Every time an instance is created |
| Purpose | Initialize static members | Initialize instance members |
| Inheritance | Not inherited | Can be inherited |
Multiple Instance Creation Example
using System;
class Counter {
static int totalObjects = 0;
int instanceId;
static Counter() {
Console.WriteLine("Static constructor called - initializing class");
totalObjects = 0;
}
public Counter() {
totalObjects++;
instanceId = totalObjects;
Console.WriteLine($"Instance constructor called - Object #{instanceId} created");
}
public void DisplayInfo() {
Console.WriteLine($"Instance ID: {instanceId}, Total Objects: {totalObjects}");
}
static void Main(string[] args) {
Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();
c1.DisplayInfo();
c2.DisplayInfo();
c3.DisplayInfo();
}
}
The output of the above code is −
Static constructor called - initializing class Instance constructor called - Object #1 created Instance constructor called - Object #2 created Instance constructor called - Object #3 created Instance ID: 1, Total Objects: 3 Instance ID: 2, Total Objects: 3 Instance ID: 3, Total Objects: 3
Conclusion
Static constructors execute once per class to initialize static members, while instance constructors run each time an object is created to initialize instance data. Static constructors cannot have parameters or access modifiers, whereas instance constructors offer full flexibility for object initialization.
