What is a Static Constructor?
Static constructors are used to initialize static members of the class or to perform particular actions that need to be performed only once. A normal constructor is called each time an instance of the class is created. But in some cases, we might want the initialization of some members to happen only once in the entire application life.
A Class or struct can have only one static constructor. It is automatically called before the first instance of the class is created or any static member of the class is referred.
It can’t have any access modifiers or parameters, nor it can be inherited or overloaded. You can only have one single static constructor for a class.
We can never know when the static constructor is called. Nor the user has any control over its call. Its called over by the CLR / Runtime over a period of time during the application execution when some criteria are met (such as the ones mentioned before) and calls implicitly.
using System;
class Program
{
static void Main()
{
HelloWorld hw = new HelloWorld();
hw.PrintMessage();
}
}
class HelloWorld
{
// Static constructor
static HelloWorld()
{
Console.WriteLine("Initializing the HelloWorld class...");
}
public void PrintMessage()
{
Console.WriteLine("Hello, World!");
}
}
What is a Singleton?
A Singleton is one of the twenty three design patterns. A Singleton class is a class instance that is created only once in its entire lifecycle and only one instance is to be used across the system in the entire flow.
To create a Singleton class, we will first create a private constructor and store an instance of the class to return in a static private variable – that is returned by a public GetInstance() method.
using System;
public sealed class HelloWorld
{
private static HelloWorld instance;
// Private constructor to prevent instantiation outside the class
private HelloWorld()
{
Console.WriteLine("Initializing the HelloWorld singleton...");
}
// Public method to access the singleton instance
public static HelloWorld GetInstance()
{
if (instance == null)
{
instance = new HelloWorld();
}
return instance;
}
public void PrintMessage()
{
Console.WriteLine("Hello, World! I'm a singleton.");
}
}
class Program
{
static void Main()
{
// Access the singleton instance
HelloWorld singleton = HelloWorld.GetInstance();
singleton.PrintMessage();
}
}
Summary – Differences between Static Constructor and Singleton
- A static constructor doesn’t have any access modifier (public, protected..) nor is allowed.
- Static constructors are run once for a type at an unknown time before any of the static members are called.
- Adding a static constructor doesn’t stop the class from being initialized, it is just that the members which are desired to be instantiated only once are placed inside a static constructor (for example a HttpClient)
- A Singleton class has a private constructor by design, which stops it from being instantiated and hence only a single instance of the type can be circulated at any time.