fork download
  1. using System;
  2.  
  3. public class Test
  4. {
  5. public static void Main()
  6. {
  7. new TestEvent();
  8. }
  9. }
  10.  
  11. public class TestEvent
  12. {
  13. public event Action Event;
  14.  
  15. public TestEvent()
  16. {
  17. Action d1 = Print;
  18. Action d2 = Print;
  19.  
  20. // The delegates are distinct
  21. Console.WriteLine("d1 and d2 are the same: {0}", object.ReferenceEquals(d1, d2));
  22.  
  23. Event += d1;
  24. Event -= d2;
  25.  
  26. // But the second one is able to remove the first one :-)
  27. // (an event when is empty is null)
  28. Console.WriteLine("d2 was enough to remove d1: {0}", Event == null);
  29. }
  30.  
  31. public void Print()
  32. {
  33. Console.WriteLine("TestEvent");
  34. }
  35. }
  36.  
Success #stdin #stdout 0.03s 24120KB
stdin
Standard input is empty
stdout
d1 and d2 are the same: False
d2 was enough to remove d1: True