Implementing C# Events

C#
5 min read

A common requirement when designing and implementing software is the ability to respond to certain code conditions, or at key points in an application's execution lifecycle. These conditions and lifecycle points can be thought of as events, where some action occurs and some other piece of code can react to it, or some other action then happens. An area where these types of systems commonly appear are graphical user interface libraries: application developers can subscribe to a "click" event to execute some code when a user clicks a button, for instance.

So how might we go about implementing this in our own code? A trap I see new developers, who are still unfamiliar with C#, is the heavy use of custom delegate management for creating event systems.

C# has a data type called delegate, which are a type of value that understands how to execute a method, and can hold a reference to that method. They are similar to interfaces in that they can define a method signature, but differ in that they can be defined as a class member, can hold a reference to a method, and can also be directly invoked by other code. To demonstrate how they may be used, we will create a simple class that will allow other developers to "hook" into it and do something when a class method is invoked.

In this example, we've defined a delegate Barked which describes a method that has a return type of void, and accepts no arguments. Additionally, we've created a class member OnBarked that can be assigned any method that matches the delegate (a void return type and accepts no parameters). When the Bark instance method is invoked, it will check if anything has been assigned to OnBarked and if so, it will call it. When we run the following code example, we see the text The dog barked in the output:

using System;

namespace EventExample
{

    public class Dog
    {
        public delegate void Barked();

        public Barked OnBarked;

        public void Bark()
        {
            this.OnBarked?.Invoke();
        }

    }

    class Program
    {
        
        static void Main(string[] args)
        {
            var dog = new Dog();

            dog.OnBarked = DogBarked;

            dog.Bark();
        }

        static void DogBarked()
        {
            Console.WriteLine("The dog barked");
        }

    }

}

Great! We've done it! Well, not quite. We have created a simple system that will allow a single method to be invoked by our code. If we attempt to have two methods invoked when our Dog class barks we can start to see where things are falling apart with this strategy:

class Program
{
    
    static void Main(string[] args)
    {
        var dog = new Dog();

        dog.OnBarked = DogBarkOne;
        dog.OnBarked = DogBarkTwo;

        dog.Bark();
    }

    static void DogBarkOne()
    {
        Console.WriteLine("The dog barked #1");
    }

    static void DogBarkTwo()
    {
        Console.WriteLine("The dog barked #2");
    }

}

When running the above code, we will only see the following output:

The dog barked #2

Because the OnBarked can only be assigned a single value, our second assignment overwrites the first and thus only the DogBarkTwo() method is called when the dog barks. If the problem is that the assignment overwrites our previous handler, why not keep a list of them? We could easily create a Barked[] list, iterate them all and invoke them right?

While it is true that this is something we could do, we can easily achieve the same thing using C#'s event features. We will keep our delegate Barked, but will change out our Barked implementation to use C#'s event keyword:

using System;

namespace EventArticle
{

    public class Dog
    {
        public delegate void Barked();

        // Adding the event keyword provides
        // us with many useful features.
        public event Barked OnBarked;


        public void Bark()
        {
            this.OnBarked?.Invoke();
        }

    }

    class Program
    {
        
        static void Main(string[] args)
        {
            var dog = new Dog();

            dog.OnBarked += DogBarkOne;
            dog.OnBarked += DogBarkTwo;

            dog.Bark();
        }

        static void DogBarkOne()
        {
            Console.WriteLine("The dog barked #1");
        }

        static void DogBarkTwo()
        {
            Console.WriteLine("The dog barked #2");
        }

    }

}

The output of our program is now what we would expect, and both methods are invoked when the dog barks:

The dog barked #1
The dog barked #2

At first glance, it does not appear that much of anything in our code has changed. Instead of assigning a value to our OnBarked, we are now using a weird += syntax. C# events are something that can be subscribed to and un-subscribed to (or adding some method to the delegate's list of methods to call, or removing a method from that list of methods).

The following snippet adds our method to the internal list of subscribers that will be notified when the dog barks. These are also referred to as event handlers:

dog.OnBarked += DogBarkOne;

We can also remove event handlers from an event using the -= syntax:

dog.OnBarked -= DogBarkOne;
dog.OnBarked -= DogBarkTwo;

Conclusion

Events and delegates can seem confusing at first, and appear to have many overlapping feature sets at first. C#'s event feature is simply a wrapper around the delegate type, and provides us with some convenient features so that we do not have to implement our own delegate management systems in order to effectively implement a robust event system. For more information, consider reading the following Microsoft documentation: Handle and Raise Events and How to: Raise and Consume Events.

If you found this article, consider sharing it with your friends or colleagues!

Comments

There are no comments. Be the first to comment!

Up next