Delegates And Events In C#

Contents

What Are Events In C#?

An event can be used to provide notifications. You can subscribe to an event if you are interested in those notifications. You can also create your own events and raise them to provide notifications when something interesting happens. The .NET Framework offers built-in types that you can use to create events. By using delegates, lambda expressions, and anonymous methods, you can create and use events in a comfortable way.

Understanding Delegates In C#

In C#, delegates form the basic building blocks for events. A delegate is a type that defines a method signature. In C++, for example, you would do this with a function pointer. In C# you can instantiate a delegate and let it point to another method. You can invoke the method through the delegate.

Below is an example of declaring a delegate and calling a method through it.

Using A Delegate In C#

 1 class Program
 2 {
 3     public delegate double MathDelegate(double value1, double value2);
 4 
 5     public static double Add(double value1, double value2)
 6     {
 7         return value1 + value2;
 8     }
 9     public static double Subtract(double value1, double value2)
10     {
11         return value1 - value2;
12     }
13 
14     public static void Main()
15     {
16         MathDelegate mathDelegate = Add;
17         var result = mathDelegate(5, 2);
18         Console.WriteLine(result);
19         // output: 7
20 
21         mathDelegate = Subtract;
22         result = mathDelegate(5, 2);
23         Console.WriteLine(result);
24         // output: 3
25 
26         Console.ReadLine();
27     }
28 
29 }

As you can see, we use the delegate keyword to tell the compiler that we are creating a delegate type.

Instantiating delegates is easy with the automatic creation of a new delegate type.

You can also use the new keyword method of instantiating a delegate

1 MathDelegate mathDelegate = new MathDelegate(Add);

An instantiated delegate is an object; you can pass it around and give it as an argument to other methods.

Multicast Delegates In C#

Another great feature of delegates is that you can combine them together. This is called multicasting. You can use the + or += operator to add another method to the invocation list of an existing delegate instance. Similarly, you can also remove a method from an invocation list by using the decrement assignment operator (- or -=). This feature forms the base for events in C#. Below is a multicast delegate example.

 1 class Program
 2 {
 3     static void Hello(string s)
 4     {
 5         Console.WriteLine("  Hello, {0}!", s);
 6     }
 7 
 8     static void Goodbye(string s)
 9     {
10         Console.WriteLine("  Goodbye, {0}!", s);
11     }
12 
13     delegate void Del(string s);
14 
15     static void Main()
16     {
17         Del a, b, c, d;
18 
19         // Create the delegate object a that references 
20         // the method Hello:
21         a = Hello;
22 
23         // Create the delegate object b that references 
24         // the method Goodbye:
25         b = Goodbye;
26 
27         // The two delegates, a and b, are composed to form c: 
28         c = a + b;
29 
30         // Remove a from the composed delegate, leaving d, 
31         // which calls only the method Goodbye:
32         d = c - a;
33 
34         Console.WriteLine("Invoking delegate a:");
35         a("A");
36         Console.WriteLine("Invoking delegate b:");
37         b("B");
38         Console.WriteLine("Invoking delegate c:");
39         c("C");
40         Console.WriteLine("Invoking delegate d:");
41         d("D");
42 
43 
44         /* Output:
45         Invoking delegate a:
46           Hello, A!
47         Invoking delegate b:
48           Goodbye, B!
49         Invoking delegate c:
50           Hello, C!
51           Goodbye, C!
52         Invoking delegate d:
53           Goodbye, D!
54         */
55 
56         Console.ReadLine();
57     }
58 }

All this is possible because delegates inherit from the System.MulticastDelegate class that in turn inherits from System.Delegate. Because of this, you can use the members that are defined in those base classes on your delegates.

For example, to find out how many methods a multicast delegate is going to call, you can use the following code:

1 int invocationCount = d.GetInvocationList().GetLength(0);

Covariance and Contravariance In C#

When you assign a method to a delegate, the method signature does not have to match the delegate exactly. This is called covariance and contravariance. Covariance makes it possible that a method has a return type that is more derived than that defined in the delegate. Contravariance permits a method that has parameter types that are less derived than those in the delegate type.

Covariance With Delegates

Here is an example of covariance,

 1 class Program
 2 {
 3     public delegate TextWriter CovarianceDel();
 4 
 5     public static StreamWriter MethodStream() { return null;  }
 6     public static StringWriter MethodString() { return null;  }
 7 
 8     static void Main()
 9     {
10         CovarianceDel del;
11 
12         del = MethodStream;
13         del = MethodString;
14 
15         Console.ReadLine();
16     }
17 }

Because both StreamWriter and StringWriter inherit from TextWriter, you can use the CovarianceDel with both methods.

Contravariance With Delegates

Below is an example of contravariance.

 1 class Program
 2 {
 3     public static void DoSomething(TextWriter textWriter) { }
 4     public delegate void ContravarianceDel(StreamWriter streamWriter);
 5 
 6     static void Main()
 7     {
 8         ContravarianceDel del = DoSomething;
 9 
10         Console.ReadLine();
11     }
12 }

Because the method DoSomething can work with a TextWriter, it surely can also work with a StreamWriter. Because of contravariance, you can call the delegate and pass an instance of StreamWriter to the DoSomething method

You can learn more about this concept here.

Lambda Expressions In C#

Sometimes the whole signature of a method can be more code than the body of a method. There are also situations in which you need to create an entire method only to use it in a delegate.

For these cases, Microsoft added some new features to C#, 2.0 anonymous methods were added. In C# 3.0, things became even better when lambda expressions were added. Lambda expression is the preferred way to go when writing new code.

Below is an example of newer lambda syntax.

 1 class Program
 2 {
 3     public delegate double MathDelegate(double value1, double value2);
 4 
 5     public static void Main()
 6     {
 7         MathDelegate mathDelegate = (x,y) => x + y;
 8         var result = mathDelegate(5, 2);
 9         Console.WriteLine(result);
10         // output: 7
11 
12         mathDelegate = (x, y) => x - y; ;
13         result = mathDelegate(5, 2);
14         Console.WriteLine(result);
15         // output: 3
16 
17         Console.ReadLine();
18     }
19 
20 }

When reading this code, you can say go or goes to for the special lambda syntax. For example, the first lambda expression in the above example is read as “x and y goes to adding x and y”.

The lambda function has no specific name as the methods. Because of this, lambda functions are called anonymous functions. You also don’t have to specify a return type explicitly. The compiler infers this automatically from your lambda. And in the case of the above example, the types of parameters x and y are also not specified explicitly.

You can create lambdas that span multiple statements. You can do this by adding curly braces around the statements that form the lambda as below example shows.

1 MathDelegate mathDelegate = (x,y) => 
2             {
3                 Console.WriteLine("Add");
4                 return x + y;
5             };

Sometimes declaring a delegate for an event feels a bit cumbersome. Because of this, the .NET Framework has a couple of built-in delegates types that you can use when declaring delegates. For the MathDelegate examples, you have used the following delegate:

1 public delegate double MathDelegate(double value1, double value2);

You can replace this delegate with one of the built-in types namely Func<int, int, int>.

like this,

 1 class Program
 2     {
 3         public static void Main()
 4         {
 5             Func<int, int, int> mathDelegate = (x,y) => 
 6             {
 7                 Console.WriteLine("Add");
 8                 return x + y;
 9             };
10 
11             var result = mathDelegate(5, 2);
12             Console.WriteLine(result);
13             // output: 7
14 
15             mathDelegate = (x, y) => x - y; ;
16             result = mathDelegate(5, 2);
17             Console.WriteLine(result);
18             // output: 3
19 
20             Console.ReadLine();
21         }
22 
23     }

The Func<...> types can be found in the System namespace and they represent delegates that return a type and take 0 to 16 parameters. All those types inherit from System.MulticaseDelegate so you can add multiple methods to the invocation list.

If you want a delegate type that doesn’t return a value, you can use the System.Action types. They can also take 0 to 16 parameters, but they don’t return a value.

Here is an example of using the Action type,

 1 class Program
 2     {
 3         public static void Main()
 4         {
 5             Action<int, int> mathDelegate = (x,y) => 
 6             {
 7                 Console.WriteLine(x + y);
 8             };
 9 
10             mathDelegate(5, 2);
11             // output: 7
12 
13             mathDelegate = (x, y) => Console.WriteLine(x - y) ;
14             mathDelegate(5, 2);
15             // output: 3
16 
17             Console.ReadLine();
18         }
19 
20     }

You can learn more about .NET built-in delegates here.

Things start to become more complex when your lambda function starts referring to variables declared outside of the lambda expression or to this reference. Normally when control leaves the scope of the variable, the variable is no longer valid. But what if a delegate refers to a local variable. To fix this, the compiler generates code that makes the life of the captured variable at least as long as the longest-living delegate. This is called a closure.

You can learn more about closure here.

Events In C#

A popular design pattern is application development is that of publish-subscribe. You can subscribe to an event and then you are notified when the publisher of the event raises a new event. This is used to establish loose coupling between components in an application.

Delegate form the basis for the event system in C#.

An event is a special kind of delegate that facilitates event-driven programming. Events are class members that cannot be called outside of the class regardless of its access specifier. So, for example, an event declared to be public would allow other classes the use of += and -= on the event, but firing the event (i.e. invoking the delegate) is only allowed in the class containing the event. Let’s see an example,

 1 //Define publisher class as Pub
 2 public class Pub
 3 {
 4     //OnChange property containing all the 
 5     //list of subscribers callback methods
 6     public event Action OnChange = delegate { };
 7 
 8     public void Raise()
 9     {
10         //Invoke OnChange Action
11         OnChange();
12     }
13 }

A method in another class can then subscribe to the event by adding one of its methods to the event delegate:

Below example shows how a class can expose a public delegate and raise it.

 1 class Program
 2 {
 3     static void Main(string[] args)
 4     {
 5         //Initialize pub class object
 6         Pub p = new Pub();
 7 
 8         //register for OnChange event - Subscriber 1
 9         p.OnChange += () => Console.WriteLine("Subscriber 1!");
10         //register for OnChange event - Subscriber 2
11         p.OnChange += () => Console.WriteLine("Subscriber 2!");
12 
13         //raise the event
14         p.Raise();
15 
16         //After this Raise() method is called
17         //all subscribers callback methods will get invoked
18 
19         Console.WriteLine("Press enter to terminate!");
20         Console.ReadLine();
21     }
22 }

Even though the event is declared public, it cannot be directly fired anywhere except in the class containing it.

By using event keyword compiler protects our field from unwanted access.

And,

It does not permit the use of = (direct assignment of a delegate). Hence, your code is now safe from the risk of removing previous subscribers by using = instead of +=.

Also, you may have noticed special syntax of initializing the OnChange field to an empty delegate like this delegate { }. This ensures that our OnChange field will never be null. Hence, we can remove the null check before raising the event, if no other members of the class making it null.

When you run the above program, your code creates a new instance of Pub, subscribes to the event with two different methods and raises the event by calling p.Raise. The Pub class is completely unaware of any subscribers. It just raises the event.

You can also read my article Publish Subscribe Design Pattern In C# for more in-depth knowledge of this concept.

Further Reading

  • Publish Subscribe Design Pattern In C# - Publish Subscribe or Pub-Sub is a design pattern that allows loose coupling between the application components. This post explains the implementation detail of Pub-Sub using Delegates, EventHandlers and Event keyword in C#.

  • C# Generic Delegates Func, Action, and Predicate - C# 3.0 includes built-in generic delegate types Func, Action, and Predicate, so that you don’t need to define custom delegates. In this post, you will learn about these built-in delegates in C#.

  • C# 7.0 Expression Bodied Members by Christian Nagel - C# 6 introduced expression bodied members with methods and properties. This feature has been enhanced with C# 7.0 to allow expression bodied members with constructors, destructors, and also property accessors. This article gives a review on expression bodied members with C# 6, and shows the new options with C# 7.0. This is much similar to Lambda Expressions we discussed in the post before.

  • Understanding Delegates and Higher-Order Functions in C# by Zoran Horvat - One of the core concepts of functional programming is functions and the ability of using, manipulating and passing functions as if they were objects. C# delegates allows us to create types to store functions with a specific signature. This post introduces Functional programming concepts using C# Delegates.

References

Delegates And Events In C#
Share this

Subscribe to Code with Shadman