There might be times when you'll want your event to perform the same actions for all objects. One way to do that is to add a same event handler to the event for each object separately, but the easier (lazier) way is to simply create a static event,
  1. using System;
  2. // I will use a custom delegate for demonstration,
  3. // because I want to pass only the object itself
  4. delegate void MyEventHandler(object sender);
  5. class MyClass
  6. {
  7. // Our static event
  8. public static event MyEventHandler MyEvent;
  9. // Our event-invoking function
  10. public void EventInvokingCode(int num)
  11. {
  12. // Invoke our event when num is odd.
  13. // It's also good to check if event is null
  14. if (num % 2 == 1 && MyEvent != null)
  15. {
  16. // We'll pass the object itself
  17. MyEvent(this);
  18. }
  19. }
  20. // I want to display object's name when our event occurs,
  21. // so I'll create a property called "Name"
  22. public string Name { get; set; }
  23. }
  24. class main
  25. {
  26. static void onEvent1(object sender)
  27. {
  28. string objName = ((MyClass)sender).Name;
  29. Console.WriteLine("Object's name: " + objName);
  30. }
  31. static void onEvent2(object sender)
  32. {
  33. string objName = ((MyClass)sender).Name.ToLower();
  34. Console.WriteLine("Object's name: " + objName);
  35. }
  36. static void Main()
  37. {
  38. // Four object of type MyClass
  39. MyClass obj1 = new MyClass();
  40. obj1.Name = "C SHARP IS";
  41. MyClass obj2 = new MyClass();
  42. obj2.Name = "NOT";
  43. MyClass obj3 = new MyClass();
  44. obj3.Name = "GREAT";
  45. MyClass obj4 = new MyClass();
  46. obj4.Name = "MONKEY BUSSINES";
  47. // Add a method to our static event:
  48. MyClass.MyEvent += onEvent1;
  49. // I'll add another handler to propagate
  50. // the useful Lambda Expressions
  51. MyClass.MyEvent += (object sender) =>
  52. {
  53. int lenght = ((MyClass)sender).Name.Length;
  54. Console.WriteLine("*name len: " + lenght);
  55. };
  56. // Let's call our event-invoking function
  57. obj1.EventInvokingCode(1);
  58. obj2.EventInvokingCode(4);
  59. obj3.EventInvokingCode(3);
  60. obj4.EventInvokingCode(8);
  61. // Our static event doesn't have much meaning
  62. // until we use it as events are meant to be used:
  63. // I'll remove and add handlers
  64. MyClass.MyEvent -= onEvent1;
  65. MyClass.MyEvent += onEvent2;
  66. // Let's call our event-invoking function again
  67. Console.WriteLine("\n\nAnother run:\n");
  68. obj1.EventInvokingCode(5);
  69. obj2.EventInvokingCode(7);
  70. obj3.EventInvokingCode(2);
  71. obj4.EventInvokingCode(9);
  72. }
  73. }
The program produces the following output,
Object's name: C SHARP IS
*name len: 10
Object's name: GREAT
*name len: 5

Another run:

*name len: 10
Object's name: c sharp is
*name len: 3
Object's name: not
*name len: 15
Object's name: monkey bussines
Notice that in the second run the event handler displaying the length of object's name is executed first. This is because we added that handler before the new one.

Unfortunately there is no simple way to remove an anonymous method from a collection of added handlers. We can remove all handlers with a foreach loop, but if we want to remove only a specific anonymous method, we have to keep a track of its index in the collection. We can get the collection by using Delegate.GetInvocationList method.

Static event would be useful, if, for example, you had a list of items and you'd want them to invoke some actions when a certain condition is reached or change is made. This could all still be done with a simple function call, but it's better to add and remove actions in a form of events than to run through a set of conditional statements on each call.
Thanks for reading!