1 00:00:00,390 --> 00:00:00,750 ‫All right. 2 00:00:00,750 --> 00:00:02,100 ‫So what is a delegate? 3 00:00:02,250 --> 00:00:07,330 ‫Well, in simple terms, a delegate is a type that can hold a reference to a method. 4 00:00:07,350 --> 00:00:11,310 ‫And when you call the delegate, the method referenced will get called. 5 00:00:11,790 --> 00:00:18,150 ‫Imagine we are developing a UI library that other developers can use to build mobile apps, and one 6 00:00:18,150 --> 00:00:22,110 ‫of the UI elements that other developers can use is a button. 7 00:00:22,140 --> 00:00:28,110 ‫If we look at the code behind the button class used to create the green button on the screen, a button 8 00:00:28,110 --> 00:00:35,490 ‫like this will have the following properties a string called text with the value sent, a color called 9 00:00:35,730 --> 00:00:45,330 ‫back color, which is something greenish and of course two, it's called size W and size H for the width 10 00:00:45,330 --> 00:00:47,190 ‫and height of the button. 11 00:00:47,400 --> 00:00:53,940 ‫Now, what is missing here is a method that contains the code that needs to be executed once the button 12 00:00:53,940 --> 00:00:54,690 ‫is clicked. 13 00:00:54,720 --> 00:00:56,310 ‫Now we have a problem here. 14 00:00:56,940 --> 00:01:03,720 ‫We, as the developers who made this UI system, cannot tell what the other developers using our UI 15 00:01:03,750 --> 00:01:06,990 ‫system want the button to do when it's clicked. 16 00:01:07,020 --> 00:01:13,230 ‫That's why we need to provide the on click event as a delegate and not as a normal method. 17 00:01:13,260 --> 00:01:19,800 ‫And since a delegate is a variable that can store a reference to a method, as we heard earlier, other 18 00:01:19,800 --> 00:01:26,670 ‫developers can write their own implementation inside a method that contains the logic they want to be 19 00:01:26,670 --> 00:01:33,110 ‫executed on click and assign it to the buttons on click delegate slash event. 20 00:01:33,120 --> 00:01:37,590 ‫We provide it our own click delegate can be defined like this. 21 00:01:38,840 --> 00:01:41,900 ‫So now let's break it down a bit by bit. 22 00:01:41,930 --> 00:01:49,220 ‫We will start by writing the access specified to this private keyword, followed by the delegate keyword, 23 00:01:49,220 --> 00:01:53,270 ‫which tells the compiler that we are defining a new type. 24 00:01:53,540 --> 00:01:58,190 ‫And finally, we need to specify the type of methods our delegate can store. 25 00:01:58,190 --> 00:02:06,590 ‫And by type I mean the return type and the parameters on Click Delegate is the name of the delegate 26 00:02:06,590 --> 00:02:07,640 ‫we just defined. 27 00:02:07,670 --> 00:02:12,790 ‫Now the next step is to actually create a variable of its new delegate type. 28 00:02:12,800 --> 00:02:17,000 ‫So we define it just like we define any variable in C-sharp. 29 00:02:17,000 --> 00:02:24,350 ‫By starting with public, then the type of the variable we are creating, which is on on click delegate. 30 00:02:24,350 --> 00:02:27,020 ‫And lastly, the variable name which is on click. 31 00:02:27,230 --> 00:02:33,140 ‫So again, we defined a public variable of type on Click Delegate and we named it on Click. 32 00:02:33,850 --> 00:02:41,620 ‫This variable can hold or store a reference to any method as long as it has void as the return type 33 00:02:41,620 --> 00:02:43,300 ‫and takes no parameter. 34 00:02:43,420 --> 00:02:48,820 ‫Otherwise we'll get an error similar to when we try to store a float inside an in variable. 35 00:02:49,060 --> 00:02:51,790 ‫So how can we use this new delegate? 36 00:02:51,940 --> 00:02:59,680 ‫Well, let's say we drag the dropped a button into our canvas in the ID we are using to develop this 37 00:02:59,680 --> 00:03:05,200 ‫mobile app using our UI system which resulted in the creation of a button object. 38 00:03:05,230 --> 00:03:08,830 ‫Let's assume the name of this button is sent button. 39 00:03:08,830 --> 00:03:12,860 ‫So let's first add the logic we want to execute when the button is clicked. 40 00:03:12,880 --> 00:03:18,600 ‫In our case, we want to connect to the network, send a message, then display a message to the user. 41 00:03:18,610 --> 00:03:24,700 ‫So now we have a normal method called Send Button Click that is not yet connected to the button in any 42 00:03:24,700 --> 00:03:25,180 ‫way. 43 00:03:25,300 --> 00:03:31,990 ‫To fix that, we need to assign our method to the on click variable of type on Click Delegate. 44 00:03:32,630 --> 00:03:38,960 ‫Note that we wrote our method name without the brackets because we are not calling it, we are just 45 00:03:38,960 --> 00:03:41,750 ‫storing its reference in the correct delegate. 46 00:03:41,960 --> 00:03:50,180 ‫Now under the hood, if the mouse was hovering over the button and the mouse was clicked, our UI system 47 00:03:50,180 --> 00:03:56,930 ‫will call the on click delegate of our send button that in return will call any method that was assigned 48 00:03:56,930 --> 00:03:57,740 ‫to it. 49 00:03:58,190 --> 00:04:05,180 ‫In this case, it will call the send button click and the message sent dialog will be displayed. 50 00:04:05,210 --> 00:04:11,360 ‫This way whoever uses our UI system can create their own methods, then assign their methods to our 51 00:04:11,360 --> 00:04:15,080 ‫delegate and we can call them whenever the conditions are right. 52 00:04:15,320 --> 00:04:17,530 ‫We are not limited to on click events. 53 00:04:17,540 --> 00:04:22,580 ‫We can even create delegates for when the application starts or closes, for example. 54 00:04:23,320 --> 00:04:29,020 ‫Well, this was a very abstract example, but this is a real use case of delegates in general. 55 00:04:29,050 --> 00:04:34,360 ‫So next we will learn how to use one of the delegates that already exists in C-sharp. 56 00:04:34,390 --> 00:04:40,750 ‫And after that, we will start creating our own delegates, similar to how we did it in this example. 57 00:04:40,750 --> 00:04:42,280 ‫So see you in the next video.