Static Event in C#


There might be times when you'll want your event to perform 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:

using System;

// I will use a custom delegate for demonstration,
// because I want to pass only the object itself
delegate void MyEventHandler(object sender);

class MyClass
{
    // Our static event
    public static event MyEventHandler MyEvent;

    // Our event-invoking function
    public void EventInvokingCode(int num)
    {
        // Invoke our event when num is odd.
        // It's also good to check if event is null
        if (num % 2 == 1 && MyEvent != null)
        {
            // We'll pass the object itself
            MyEvent(this);
        }
    }

    // I want to display object's name when our event occurs,
    // so I'll create a property called "Name"
    public string Name { get; set; }
}

class main
{
    static void onEvent1(object sender)
    {
        string objName = ((MyClass)sender).Name;
        Console.WriteLine("Object's name: " + objName);
    }

    static void onEvent2(object sender)
    {
        string objName = ((MyClass)sender).Name.ToLower();
        Console.WriteLine("Object's name: " + objName);
    }

    static void Main()
    {
        // Four object of type MyClass
        MyClass obj1 = new MyClass();
        obj1.Name = "C SHARP IS";
        MyClass obj2 = new MyClass();
        obj2.Name = "NOT";
        MyClass obj3 = new MyClass();
        obj3.Name = "GREAT";
        MyClass obj4 = new MyClass();
        obj4.Name = "MONKEY BUSSINES";

        // Add a method to our static event:
        MyClass.MyEvent += onEvent1;

        // I'll add another handler to propagate
        // the useful Lambda Expressions
        MyClass.MyEvent += (object sender) =>
        {
            int lenght = ((MyClass)sender).Name.Length;
            Console.WriteLine("*name len: " + lenght);
        };

        // Let's call our event-invoking function
        obj1.EventInvokingCode(1);
        obj2.EventInvokingCode(4);
        obj3.EventInvokingCode(3);
        obj4.EventInvokingCode(8);

        // Our static event doesn't have much meaning
        // until we use it as events are meant to be used:
        // I'll remove and add handlers
        MyClass.MyEvent -= onEvent1;
        MyClass.MyEvent += onEvent2;

        // Let's call our event-invoking function again
        Console.WriteLine("\n\nAnother run:\n");
        obj1.EventInvokingCode(5);
        obj2.EventInvokingCode(7);
        obj3.EventInvokingCode(2);
        obj4.EventInvokingCode(9);
    }
}

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 length of object's name is executed first. This is because we added that handler before new one.
Unfortunately there is no simple way to remove an anonymous method from collection of added handlers. We can remove all handlers with 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 you'd, for example, had a list of items and you'd want them to invoke some actions when 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 set of conditional statements on each call.

Thanks for reading!

Up Next
    Ebook Download
    View all
    Learn
    View all