csharp / beginner
Snippet
Global Instance Coordination
The Singleton pattern ensures that a class has only one instance and provides a global point of access to it. This is useful for managing shared state like application settings.
snippet.cs
csharp
1
2
3
4
5
6
7
8
9
10
public class ConfigurationManager{// A static property that holds the single instancepublic static ConfigurationManager Instance { get; } = new ConfigurationManager();// Private constructor prevents other classes from creating new instancesprivate ConfigurationManager() { }public string AppName = "MyCSharpApp";}
Breakdown
1
public static ConfigurationManager Instance { get; } = new ConfigurationManager();
Creates the one and only instance of this class.
2
private ConfigurationManager() { }
Prevents instantiation from outside the class via the 'new' keyword.