-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleton.cs
More file actions
34 lines (27 loc) · 1.15 KB
/
Singleton.cs
File metadata and controls
34 lines (27 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void Main()
{
var properties1 = Properties.Instance;
var properties2 = Properties.Instance;
$"Using {nameof(properties1)}, the application name is: {properties1.ApplicationName}".Dump();
$"Using {nameof(properties2)}, the application name is: {properties2.ApplicationName}".Dump();
}
// Singleton class, allows for at most one unique instance of an object to exist
public class Properties
{
// Unique instance that can be accessed by the outside
// Using the lazy initialization, we only create the element once: when requested
// for the first time
private static Lazy<Properties> _lazyInstance = new Lazy<Properties>(() => new Properties());
public string ApplicationName { get; private set; } = string.Empty;
public static Properties Instance { get => _lazyInstance.Value; }
// The constructor must be private to ensure that no other instance can be
// created from the outside
private Properties()
=> LoadProperties();
// This may be the parsing of a file, a database or else
private void LoadProperties()
{
ApplicationName = "Singleton Demo";
"Properties loaded !".Dump();
}
}