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 the difference between Static class and Singleton instance in C#?
A static class is declared using the static keyword and contains only static members that belong to the class itself rather than any instance. A Singleton is a design pattern that ensures only one instance of a class can be created and provides global access to that instance.
Static Class
A static class cannot be instantiated and all its members must be static. It is loaded automatically by the .NET Framework before the class is referenced for the first time −
Syntax
public static class ClassName {
public static void Method() {
// static method implementation
}
}
Example
using System;
public static class Calculator {
public static int Add(int a, int b) {
return a + b;
}
public static int Multiply(int a, int b) {
return a * b;
}
}
public class Program {
public static void Main() {
Console.WriteLine("Addition: " + Calculator.Add(5, 3));
Console.WriteLine("Multiplication: " + Calculator.Multiply(4, 6));
}
}
The output of the above code is −
Addition: 8 Multiplication: 24
Singleton Pattern
The Singleton pattern restricts instantiation of a class to one object and provides a global point of access to that instance −
Syntax
public class Singleton {
private static Singleton instance = null;
private Singleton() { }
public static Singleton GetInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
Example
using System;
public class DatabaseConnection {
private static DatabaseConnection instance = null;
private string connectionString;
private DatabaseConnection() {
connectionString = "Server=localhost;Database=MyDB";
Console.WriteLine("Database connection created");
}
public static DatabaseConnection GetInstance() {
if (instance == null) {
instance = new DatabaseConnection();
}
return instance;
}
public void Connect() {
Console.WriteLine("Connected to: " + connectionString);
}
}
public class Program {
public static void Main() {
DatabaseConnection db1 = DatabaseConnection.GetInstance();
DatabaseConnection db2 = DatabaseConnection.GetInstance();
db1.Connect();
db2.Connect();
Console.WriteLine("Same instance: " + (db1 == db2));
}
}
The output of the above code is −
Database connection created Connected to: Server=localhost;Database=MyDB Connected to: Server=localhost;Database=MyDB Same instance: True
Comparison
| Aspect | Static Class | Singleton |
|---|---|---|
| Nature | Keyword | Design pattern |
| Instantiation | Cannot be instantiated | Single instance created |
| Memory Location | Stack | Heap |
| Inheritance | Cannot inherit or implement interfaces | Can inherit from classes and implement interfaces |
| Object Reference | Cannot be passed as reference | Can be passed as reference |
| Disposal | No disposal needed | Supports IDisposable |
| Cloning | Cannot be cloned | Can be cloned if implemented |
Thread-Safe Singleton
Example
using System;
public sealed class ThreadSafeSingleton {
private static readonly ThreadSafeSingleton instance = new ThreadSafeSingleton();
static ThreadSafeSingleton() { }
private ThreadSafeSingleton() {
Console.WriteLine("Thread-safe singleton instance created");
}
public static ThreadSafeSingleton Instance {
get { return instance; }
}
public void DoWork() {
Console.WriteLine("Singleton is working...");
}
}
public class Program {
public static void Main() {
ThreadSafeSingleton s1 = ThreadSafeSingleton.Instance;
ThreadSafeSingleton s2 = ThreadSafeSingleton.Instance;
s1.DoWork();
Console.WriteLine("Same instance: " + (s1 == s2));
}
}
The output of the above code is −
Thread-safe singleton instance created Singleton is working... Same instance: True
Conclusion
Static classes are compile-time constructs that cannot be instantiated and contain only static members, while Singletons are runtime design patterns that ensure only one instance exists. Choose static classes for utility functions and Singletons when you need a single instance with object-oriented features like inheritance and polymorphism.
