A delegate is a type safe a function pointer that can reference a method that has the same signature as that of the delegate. You can take advantage of delegatese in C# to implement events.
![Delegate C Event Delegate C Event](/uploads/1/2/6/2/126266032/721077112.jpg)
- During the course of the early evolution of C#,the language added supportfor 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 lookat 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 placeholdersfor functions that will be called at some point.The same way that you can declare a variablethat holds an integer, or a string,you can declare a variablethat will hold a reference to a function.This gives you a way to change the functionthat will be called at run-time, that is,during the execution of your program.In other words, Delegates give you a wayto logically separate different parts of your applicationfrom each other, without having to know, in advance,what all the modules are.
![Event Event](http://aeromro.in/data1/images/c.jpg)
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 togetherand 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 applicationto both broadcast and receive messagesto and from the rest of your application,or even to other applications, or the OS itself.
For example, your application might have some codethat broadcasts a messagewhenever 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 numberof Event Publishers and Subscribers.
This pattern is very useful in scenarioswhere you need to communicateasynchronously with other code,or when you don't know, in advance,how many other interested parties there may befor 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 calledan 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 scenarioswhere you might use them in your app development.