‫All right.
‫So what is a delegate?
‫Well, in simple terms, a delegate is a type that can hold a reference to a method.
‫And when you call the delegate, the method referenced will get called.
‫Imagine we are developing a UI library that other developers can use to build mobile apps, and one
‫of the UI elements that other developers can use is a button.
‫If we look at the code behind the button class used to create the green button on the screen, a button
‫like this will have the following properties a string called text with the value sent, a color called
‫back color, which is something greenish and of course two, it's called size W and size H for the width
‫and height of the button.
‫Now, what is missing here is a method that contains the code that needs to be executed once the button
‫is clicked.
‫Now we have a problem here.
‫We, as the developers who made this UI system, cannot tell what the other developers using our UI
‫system want the button to do when it's clicked.
‫That's why we need to provide the on click event as a delegate and not as a normal method.
‫And since a delegate is a variable that can store a reference to a method, as we heard earlier, other
‫developers can write their own implementation inside a method that contains the logic they want to be
‫executed on click and assign it to the buttons on click delegate slash event.
‫We provide it our own click delegate can be defined like this.
‫So now let's break it down a bit by bit.
‫We will start by writing the access specified to this private keyword, followed by the delegate keyword,
‫which tells the compiler that we are defining a new type.
‫And finally, we need to specify the type of methods our delegate can store.
‫And by type I mean the return type and the parameters on Click Delegate is the name of the delegate
‫we just defined.
‫Now the next step is to actually create a variable of its new delegate type.
‫So we define it just like we define any variable in C-sharp.
‫By starting with public, then the type of the variable we are creating, which is on on click delegate.
‫And lastly, the variable name which is on click.
‫So again, we defined a public variable of type on Click Delegate and we named it on Click.
‫This variable can hold or store a reference to any method as long as it has void as the return type
‫and takes no parameter.
‫Otherwise we'll get an error similar to when we try to store a float inside an in variable.
‫So how can we use this new delegate?
‫Well, let's say we drag the dropped a button into our canvas in the ID we are using to develop this
‫mobile app using our UI system which resulted in the creation of a button object.
‫Let's assume the name of this button is sent button.
‫So let's first add the logic we want to execute when the button is clicked.
‫In our case, we want to connect to the network, send a message, then display a message to the user.
‫So now we have a normal method called Send Button Click that is not yet connected to the button in any
‫way.
‫To fix that, we need to assign our method to the on click variable of type on Click Delegate.
‫Note that we wrote our method name without the brackets because we are not calling it, we are just
‫storing its reference in the correct delegate.
‫Now under the hood, if the mouse was hovering over the button and the mouse was clicked, our UI system
‫will call the on click delegate of our send button that in return will call any method that was assigned
‫to it.
‫In this case, it will call the send button click and the message sent dialog will be displayed.
‫This way whoever uses our UI system can create their own methods, then assign their methods to our
‫delegate and we can call them whenever the conditions are right.
‫We are not limited to on click events.
‫We can even create delegates for when the application starts or closes, for example.
‫Well, this was a very abstract example, but this is a real use case of delegates in general.
‫So next we will learn how to use one of the delegates that already exists in C-sharp.
‫And after that, we will start creating our own delegates, similar to how we did it in this example.
‫So see you in the next video.