Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace SingletonEasy
- {
- class MyClass
- {
- static private MyClass instance = null;
- static public MyClass Instance
- {
- get
- {
- //if (instance == null) instance = new MyClass();
- //return instance;
- return instance ??= new MyClass(); //null-coalescing
- }
- }
- public void Metodo() { Console.WriteLine("metodo richiamato"); }
- private MyClass() { Console.WriteLine("Oggetto creato"); }
- }
- class Program
- {
- static void Main(string[] args)
- {
- MyClass.Instance.Metodo();
- MyClass.Instance.Metodo();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement