fork(1) download
  1. using System;
  2.  
  3. public class MySingletonedObject
  4. {
  5. private static object me = new object();
  6. private int customData;
  7. private static MySingletonedObject sinobj;
  8. public static MySingletonedObject GetSingleton(int data)
  9. {
  10. lock (me)
  11. {
  12. if (sinobj == null)
  13. {
  14. sinobj = new MySingletonedObject(data);
  15. }
  16. }
  17. return sinobj;
  18. }
  19.  
  20. public MySingletonedObject(int newData)
  21. {
  22. customData = newData;
  23. }
  24.  
  25. public void Print()
  26. {
  27. Console.WriteLine(customData.ToString());
  28. }
  29. }
  30.  
  31. public class Test
  32. {
  33. public static void Main()
  34. {
  35. MySingletonedObject.GetSingleton(1).Print();
  36. MySingletonedObject.GetSingleton(6).Print();
  37. }
  38. }
Success #stdin #stdout 0.02s 16108KB
stdin
Standard input is empty
stdout
1
1