Join Joe Marini for an in-depth discussion in this video Overview of C# delegates, events, and lambdas, part of C# Delegates, Events, and Lambdas.
- View Offline
- During the course of the early evolution of C#, the language added support for various ways of defining functions. In C# 1.0, the core concept of the Delegate was introduced. In C# 2.0, the ability to define a Delegate, using an anonymous syntax, was added. Then, in C# 3.0, the concept of Lambda functions was added. Let's take a brief look at each of these three function types, what they are, how they work, and, most importantly, why you would want to use them.
First up are Delegates. You can think of Delegates as simply being placeholders for functions that will be called at some point. The same way that you can declare a variable that holds an integer, or a string, you can declare a variable that will hold a reference to a function. This gives you a way to change the function that will be called at run-time, that is, during the execution of your program. In other words, Delegates give you a way to logically separate different parts of your application from each other, without having to know, in advance, what all the modules are.
Instead of calling a single hard-coded function, you can swap out the function you want to call, while the program is running, based upon your program's needs. Even better, Delegates can be dynamically chained together and called in order, one after the other. We'll examine that more in-depth later in the course. Next are Events. Events are based on Delegates, and provide a way for your application to both broadcast and receive messages to and from the rest of your application, or even to other applications, or the OS itself.
For example, your application might have some code that broadcasts a message whenever something interesting happens, the value of a variable changes, or the user has interacted with a UI element. It's really up to you. That code is the Broadcaster, or sometimes it's called the Publisher of the Event. That Event might be listened to by another application, or, perhaps, just by another part of your program. These functions are called Listeners or Subscribers. Your application can have any number of Event Publishers and Subscribers.
This pattern is very useful in scenarios where you need to communicate asynchronously with other code, or when you don't know, in advance, how many other interested parties there may be for various changes in your program. Finally, we have Lambda functions. Lambdas are essentially the same as regular Delegates, but they are written using a more concise syntax. They provide another way of writing something called an anonymous function, which we'll see more of later in the course. Okay, now that we've covered what these function types are, in the next movie, let's consider some scenarios where you might use them in your app development.
Start watching to learn how to adapt this flexible set of programming tools to your own C# programming needs.