Events and Delegates are two topics in the .NET Framework that are truly misunderstood and many times not understood at all. This post will attempt to explain what events are, how they are related to delegates, and how to use them in your code.
What Are Events?
Events are a way for objects in the .NET Framework or your own classes to notify other classes or objects when something happens. Events follow the subscribe/unsubscribe model and it is valid to do so during runtime, not just compile time.
What Are Delegates?
Delegates are inseparable from events but are not events. It is an important distinction. A delegate represents a reference to a method with a pre-defined parameter list and return type. Delegates are assigned to events when instantiated and the signature of the delegate must match the signature of the event. Delegates are the foundation for events.
A Simple Example
In case the concept is not clear in your mind, here is a short example. This example will perform the outcome of the FizzBuzz test. A loop will increment a counter from 1 to 100. If the counter is a multiple of 3, it will print Fizz. If the counter is a multiple of 5, it will print Buzz. If the counter is a multiple of 3 and 5, it will print FizzBuzz. Otherwise, it will print the counter.
Listing 1.1 – Program.cs
class Program
{
static void Main(string[] args)
{
FizzBuzz fb = new FizzBuzz();
FizzBuzzDelegate dlg = new FizzBuzzDelegate(fb.DefaultFizzBuzzPrinter);
fb.OnCounterChanged += fb.GetFizzBuzzValue;
for(int i = 1; i <= 100; i++)
{
dlg(i);
}
Console.ReadKey();
}
}
The code in Program.cs wires everything up and then performs the loop for the counter. Notice that the delegate is what is actually called, not the event.
Listing 1.2 – FizzBuzz.cs
public class FizzBuzz
{
public delegate string FizzBuzzDelegate(int counter);
public event FizzBuzzDelegate OnCounterChanged;
public string DefaultFizzBuzzPrinter(int counter)
{
string output = string.Empty;
if (OnCounterChanged != null)
{
Console.WriteLine(OnCounterChanged(counter));
}
return output;
}
public string GetFizzBuzzValue(int counter)
{
string counterOutput = $"{counter} ";
if (counter % 3 == 0)
{
counterOutput += "Fizz";
}
if (counter % 5 == 0)
{
counterOutput += "Buzz";
}
return counterOutput;
}
}
The most important thing here is the way the event is triggered. The delegate calls the event.
Conclusion
Events and delegates are not as difficult as you might think. However, they are rarely used and that adds to their mystique. In practice, they are very simple because they follow a very small set of rules.