fork download
  1. using System;
  2.  
  3. class Singleton {
  4. private static Singleton _instance;
  5. private int _counter;
  6. private Singleton() {
  7. _instance = null;
  8. this._counter = 0;
  9. }
  10.  
  11. public static Singleton Instance{
  12. get {
  13. if (_instance == null) _instance = new Singleton();
  14. return _instance;
  15. }
  16. }
  17.  
  18. public void Increment() {
  19. this._counter++;
  20. Console.WriteLine(this._counter);
  21. }
  22. }
  23.  
  24.  
  25. public class Test
  26. {
  27. public static void Main()
  28. {
  29. var first = Singleton.Instance;
  30. first.Increment();
  31.  
  32. var second = Singleton.Instance;
  33. second.Increment();
  34. }
  35. }
Success #stdin #stdout 0.05s 23960KB
stdin
Standard input is empty
stdout
1
2