public class Singleton private static readonly Singleton instance new

 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
public class Singleton
{
private static readonly Singleton instance = new Singleton();
public static string text = "hello";
public string Date { get; private set; }
private Singleton()
{
Console.WriteLine($"Singleton ctor {DateTime.Now.TimeOfDay}");
Date = System.DateTime.Now.TimeOfDay.ToString();
}
public static Singleton GetInstance()
{
Console.WriteLine($"GetInstance {DateTime.Now.TimeOfDay}");
Thread.Sleep(500);
return instance;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine($"Main {DateTime.Now.TimeOfDay}");
Console.WriteLine(Singleton.text);
Console.Read();
}
}