Advertisement
yakovmonarh

Singleton. Lazy реализация.

Aug 20th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.90 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Linq;
  4.  
  5. class Program
  6. {
  7.    static void Main(string[] args)
  8.    {
  9.     Console.WriteLine("Main: {0}", DateTime.Now.TimeOfDay);
  10.     Console.WriteLine("Singleton: {0}", Singleton.text);
  11.    
  12.     Singleton singleton = Singleton.GetInstance();
  13.     Console.WriteLine("Date: {0}", singleton.Date);
  14.    
  15.     Console.ReadLine();
  16.    }
  17. }
  18.  
  19. class Singleton
  20. {
  21.     public static string text = "Hello";
  22.    
  23.     public string Date{get; private set;}
  24.    
  25.     private Singleton()
  26.     {
  27.         Console.WriteLine("Singleton ctor: {0}", DateTime.Now.TimeOfDay);
  28.         this.Date = DateTime.Now.TimeOfDay.ToString();
  29.     }
  30.    
  31.     public static Singleton GetInstance()
  32.     {
  33.         Console.WriteLine("Get instance: {0}", DateTime.Now.TimeOfDay);
  34.         return Nested.instance;
  35.     }
  36.    
  37.     private class Nested
  38.     {
  39.         static Nested(){}
  40.         internal static readonly Singleton instance = new Singleton();
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement