Events, delegates and lambdas play an important role in the .NET framework but can be difficult to understand as developers first get started with the C# language. If you’re interested in gaining a more thorough understanding of how these C# technologies work then you’re in the right place! This course focuses solely on events, delegates and lambdas and provides insight into how they can be used in your .NET applications using the C# language.
Creating Delegates, Events and EventArgs Now that you've seen the role that events and delegates play in the. NET framework, it's now time to take it up a notch and see how we can create our own custom delegates, custom events, and even custom EventArgs classes. So, in this module that's what we're going to focus on. And I'll start off by talking about what's the process for creating a delegate and provide some real world examples and some analogies to help you pick up the concept more quickly. Once we've covered delegates, we'll talk about how we can marry a delegate with an event and define that inside of a class. Then we'll talk about how we can raise events and the proper way to do that. And then we'll finally wrap up with how do we enhance our delegate signature and the parameters that are passed through it and write a custom EventArgs class? Now, as you're working with custom delegates and events, it's always important to keep the big picture in mind. We're going to be focusing on the event raiser side of the equation and talk through that process with the delegates, the events, the EventArgs, and all of that so that an event handler can ultimately get the data. So, we're going to start off, as mentioned, by talking about the glue, or what I like to call the pipeline, between the event raiser and the event handler, and how we can send data or EventArgs using that pipeline or that delegate. So, let's go ahead and get started by talking about writing custom delegates.
Handling Events Up to this point in the course, you've seen how we can work with delegates, how we can define events, and even associate delegates with events, but we haven't talked about how do you actually handle an event and how do you wire that all up. So, in this module we're going to focus on how do we handle events, and we'll revisit delegates along the way. So, we're going to start off by talking about how we can instantiate delegates and wire those up to events. From there, we'll talk about a really nice compiler feature called delegate inference. It'll save you a little bit of code. And then we'll wrap up by talking about anonymous methods and how they can be used to actually attach a method directly at the source of the event, and this will provide a nice segue into the final module in which we'll touch on lambdas. Now, as a quick review of the overall big picture, we've talked about how we can define events and that we can have event handlers, but we really haven't gone into any detail on making those event handlers, and that'll be the whole purpose of this module. So, we're going to need to have a way that the event handler can get some data dumped into it, and, of course, we do that with the delegate pipeline. That way our EventArgs can go from the event raising object over to the event handler object. So, let's go ahead and get started by talking about how we can instantiate delegates and hook those up to events.
Lambdas, Action<T> and Func<T,TResult> Lambdas are one of my favorite features in the C# language. You can use lambdas to query databases with Entity Framework or other ORMs. You can work with delegates, you can query objects with LINQ to Objects, and much more. So, in this module, we're going to talk about what lambdas are, how you can use them, and then we're going to jump into how lambdas work with some built-in. NET delegates such as Action<T> and Func<T, TResult>, and I'll talk about what those delegates are and why you'd use them. So, we're going to start off by clarifying what a lambda is and how you can use it and you'll see how they fit really nicely with delegates. Now, you've actually already seen the general concept when I talked about anonymous methods in the previous module, but you're going to see that lambdas really clean up the code and provide a more concise way to define some handler type of methods. From there, we're going to discuss the Action<T> delegate that's built in to the. NET framework, and then we'll wrap up with the Func<T, TResult>. This is another type of delegate that's built into. NET, and I'll explain the difference between Action<T> and the Func<T, TResult> and why you'd ever want to use it. So, let's get started by talking about lambdas and how they can work with delegates.