Welcome to this review of the Pluralsight course C# Events, Delegates and Lambdas by Google GDE, Microsoft MVP and Regional Director Dan Wahlin.
C# Events, Delegates & Lambdas by Dan Wahlin
This course looks at how events, delegates and lambdas work in the C# language and .NET framework. It is a nice gentle introduction to LINQ and gives the best explanation of delegates that I have ever seen.
The Role of Events, Delegates and Event Handlers
Event, Delegates and Lambdas can be hard to understand when developers are getting started with the C# language. This course aims to give you a thorough understanding of them and how they fit into the overall picture.
Dan recommends watch Scott Allen’s Accelerated C# Fundamentals if you have not used C# before.
You can think of Events, Delegates and Event Handlers using the analogy of two people talking to each other through old tin cans connected by a string!
Using a photo of this, Dan labels the event raiser, event handler, the delegate and the event args. All will become clear once you see this.
The Role of Events
We learn that events are notifications, and that the play a central role in .NET.
The simplest example of an event is a button click. Dan explains that most events have event args, which is the data that travels from one method to another.
The Role of Delegates
Delegates are often called Function Pointers, and Dan explains what this means.
He also uses the analogy of a pipeline, along with a diagram and a full description.
The Role of Event Handlers
Event Handlers receive and process data from a delegate.
Here Dan uses the tupperware container analogy.
He describes the role of an event handler in a submit button click event.
This is followed by an ASP.NET WebForms and Windows Forms demonstration in Visual Studio. The purpose of this is to illustrate that events, delegates and event handlers all go together, and you will always have a delegate involved somewhere whenever there is an event.
Creating Delegates, Events and EventArgs
In this module, Dan shows you how to create your own custom delegates, custom events and custom EventArg classes.
Dan explains that this is actually a simple process: they are defined using the delegate keyword.
We see the exact syntax required to create one, and learn that the delegate signature must be mimicked by a handler method.
Dan describes a delegate base class, with its method, target property and GetInvocationList.
Next, we learn what a multicast delegate is. It is a lot less scary than it sounds.
Now, we see how to create a delegate instance. When the compiler sees the keyword delegate, it creates a class which inherits from multicast delegate. We also learn that a delegate can be invoked in the same way as a regular method.
Next, Dan shows an example of two delegates and two instances of an event handler.
This is a lot of theory to cover, so next Dan gives several demonstrations showing this all working in practice.
3 thoughts on “C# Events, Delegates and Lambdas”
Pingback: C# Deep Dive Learning Path | Zombie Code Kill
Pingback: C# End to End Learning Path | Zombie Code Kill
Pingback: C# Tips and Traps 2 | Zombie Code Kill