csharp / beginner
Snippet
Implementing a Single Instance
The Singleton pattern ensures that a class has only one instance and provides a global point of access to it. We use a private constructor to prevent outside instantiation.
snippet.cs
csharp
1
2
3
4
5
public class AppState{public static readonly AppState Instance = new AppState();private AppState() { }}
Breakdown
1
public static readonly AppState Instance = new AppState();
Creates a single, immutable static instance of the class.
2
private AppState() { }
A private constructor prevents other classes from creating new instances.