Static Event In C#

  • Jure
  • Updated date Feb 24, 2019

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