Advertisement
yakovmonarh

Singleton через Lazy<T>

Aug 20th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.51 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(Singleton.GetInstance());
  10.     Console.ReadLine();
  11.    }
  12. }
  13.  
  14. class Singleton
  15. {
  16.     private static readonly Lazy<Singleton> lazy = new Lazy<Singleton>(()=>new Singleton());
  17.    
  18.     public string Name{get;private set;}
  19.    
  20.     private Singleton()
  21.     {
  22.         this.Name = System.Guid.NewGuid().ToString();
  23.     }
  24.    
  25.     public static Singleton GetInstance()
  26.     {
  27.         return lazy.Value;
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement