1 00:00:06,540 --> 00:00:07,010 Hello. 2 00:00:07,830 --> 00:00:10,890 In this lecture, we'll talk about Question seven. 3 00:00:11,370 --> 00:00:14,190 What are delegates in C-sharp? 4 00:00:14,250 --> 00:00:17,010 We can declare several kinds of types. 5 00:00:17,280 --> 00:00:21,930 We can declare a class or a struct to represent data and methods. 6 00:00:22,290 --> 00:00:29,790 We can declare in arms to represent some predefined discrete values, and we can also declare delegates. 7 00:00:30,180 --> 00:00:37,200 A delegate is a type whose instances hold a reference to a method which particular parameters and return 8 00:00:37,200 --> 00:00:37,650 type. 9 00:00:38,130 --> 00:00:40,110 Let's define a simple delegate. 10 00:00:45,360 --> 00:00:52,140 We declared delegates with the delegates keyword similarity as we define classes with the class, keyword 11 00:00:52,140 --> 00:00:54,330 or structs, we've this tracked keyword. 12 00:00:54,840 --> 00:01:00,960 Besides the delegates keyword, a delegated declaration looks the same as a method of declaration. 13 00:01:01,320 --> 00:01:06,900 First, the return type, then the name and finally, the list of parameters. 14 00:01:07,740 --> 00:01:08,460 All right. 15 00:01:08,640 --> 00:01:14,970 We defined our delegate type and and the variable of this type can be assigned a method that takes us 16 00:01:14,970 --> 00:01:20,070 three as parameter and also returns a string, for example. 17 00:01:20,100 --> 00:01:25,320 Both those functions can be assigned to the variable of the process string delegate type. 18 00:01:33,600 --> 00:01:39,570 We can now call the delegates and as a result, execute the functions that are stalking them. 19 00:01:51,100 --> 00:01:56,140 As you can see, the methods start in, the delegate variables have been invoked. 20 00:01:56,650 --> 00:02:02,740 The interesting feature of delegates is that we can store more than one function in one variable of 21 00:02:02,740 --> 00:02:03,700 the gate type. 22 00:02:24,290 --> 00:02:29,840 As you can see there, multicast variable holds references to two functions, not one. 23 00:02:30,530 --> 00:02:33,530 Let's see what will be the results printed to the council. 24 00:02:35,580 --> 00:02:40,680 As you can see, both functions stalled in the delegate, Baraboo has been executed. 25 00:02:41,640 --> 00:02:47,790 We can also use the plus equals operator to train another method to delegate variable. 26 00:02:55,860 --> 00:03:03,240 Now, free methods has been invoked a decade Baraboo, that holds references to more than one function. 27 00:03:03,450 --> 00:03:05,540 It's called Multicast Gallowgate. 28 00:03:06,120 --> 00:03:06,820 All right. 29 00:03:07,050 --> 00:03:11,250 In the previous lecture, we learned about funk and action types. 30 00:03:11,550 --> 00:03:12,770 What is interesting? 31 00:03:12,780 --> 00:03:15,210 Both are simply generic delegates. 32 00:03:15,840 --> 00:03:20,730 For example, this is the definition of the funk that takes two parameters. 33 00:03:21,180 --> 00:03:27,870 This delegate represents a function taking two parameters of any kind and returning some result. 34 00:03:27,960 --> 00:03:31,740 So exactly what a funk with two parameters can be present. 35 00:03:42,210 --> 00:03:48,210 Of course, instead of a lambda expression, we can also assign a pre-existing function to a variable 36 00:03:48,210 --> 00:03:51,330 of a funk, though, as you can do with any delegate. 37 00:04:00,070 --> 00:04:05,980 During the interview, you might be asked this question, what is the difference between a funk and 38 00:04:05,980 --> 00:04:07,660 another great well? 39 00:04:07,750 --> 00:04:14,020 Funk is a delegate simply defined by Microsoft, not us, to be more precise. 40 00:04:14,110 --> 00:04:20,890 Funk is a generic Gallowgate used to represent any function with given parameters, and we can't type 41 00:04:21,250 --> 00:04:24,280 a delegate is a broader concept than funk. 42 00:04:24,610 --> 00:04:29,410 We can define any delegate we want, and it doesn't need to be generic at all. 43 00:04:29,950 --> 00:04:30,670 All right. 44 00:04:30,850 --> 00:04:34,870 So we know that funks and actions are generic delegates. 45 00:04:35,380 --> 00:04:41,050 Because of that, we can represent any function we want with funk or actions, right? 46 00:04:41,560 --> 00:04:46,630 Let's see again the delegate variables we declared at the beginning of this lecture. 47 00:04:47,860 --> 00:04:51,010 We don't really need to define process string delegate. 48 00:04:51,280 --> 00:04:53,200 We could use the funk instead. 49 00:04:54,820 --> 00:04:59,680 So why bother declaring delegates if we can use Funk's instead? 50 00:05:00,400 --> 00:05:07,360 Well, first of all, delegates existed in C-sharp before funks actions and lambda expressions. 51 00:05:07,930 --> 00:05:12,610 There was a time when they were simply the only way to represent functions as objects. 52 00:05:12,850 --> 00:05:16,870 So you can still see some old gold full of custom delegates. 53 00:05:16,960 --> 00:05:22,420 Instead, the funks and actions for the simple reason that it was created a long time ago. 54 00:05:23,110 --> 00:05:29,740 Nowadays, from my practice, I would say that in the 99 percent of the cases, there is no point in 55 00:05:29,740 --> 00:05:33,520 declaring custom delegates and funks can be used instead. 56 00:05:33,940 --> 00:05:41,020 Personally, I almost never use other delegates than funks and actions, but there are some scenarios 57 00:05:41,020 --> 00:05:44,020 when custom non-genetic delegates can be useful. 58 00:05:44,500 --> 00:05:52,210 First of all, some people prefer world named dugouts to slightly cryptic funks in this interface. 59 00:05:52,300 --> 00:05:59,080 We have two methods doing the same thing, but one takes a funk and the other a custom delegate run 60 00:05:59,080 --> 00:06:04,390 command is definitely more human readable than funk of common type and bool. 61 00:06:05,050 --> 00:06:08,320 Some people think such a named delegate is better. 62 00:06:08,800 --> 00:06:11,680 Personally, I disagree for a simple reason. 63 00:06:12,310 --> 00:06:18,880 When I look at the funk of common type and bull, I know that the input to dysfunction is common type. 64 00:06:19,060 --> 00:06:25,000 And they also know that if returns a ball and they can make an educated guess that this is probably 65 00:06:25,000 --> 00:06:29,470 a boolean telling me if the command execution was successful or not. 66 00:06:29,740 --> 00:06:31,220 We've run command. 67 00:06:31,270 --> 00:06:37,510 I don't have this information and I need to go to run command, to type, to see what actually are the 68 00:06:37,510 --> 00:06:39,310 parameters and return type. 69 00:06:39,760 --> 00:06:41,130 Also, we funk. 70 00:06:41,140 --> 00:06:44,590 I can see right off the bat that this is an executable method. 71 00:06:45,040 --> 00:06:50,860 Run command could be some ordinary object as well, and they won't know that unless I go to this type 72 00:06:50,860 --> 00:06:57,130 and see the Gallowgate keyword, but I want to underline that this is a matter of personal taste. 73 00:06:57,580 --> 00:07:03,280 If for you and your team custom delegates are more readable, you should definitely use them everywhere 74 00:07:03,280 --> 00:07:04,030 you want. 75 00:07:04,690 --> 00:07:11,020 Another use case when custom delegates can have an advantage over Funk's or actions is when a specific 76 00:07:11,020 --> 00:07:13,870 delegate is used or around the application. 77 00:07:14,200 --> 00:07:20,560 This way, instead of declaring sometimes complex func everywhere, I can have a single declaration 78 00:07:20,560 --> 00:07:21,940 of a custom delegate. 79 00:07:22,420 --> 00:07:29,260 Here are defined calculate on nuance, and I can use it everywhere in the product we func. 80 00:07:29,440 --> 00:07:34,660 I would need to write this pretty long definition everywhere when I wanted to use it. 81 00:07:35,290 --> 00:07:42,040 Also, there are some use cases when we simply must use custom delegates instead of funks or actions. 82 00:07:42,430 --> 00:07:48,220 One of the examples is when we want to use them with methods with rough or out parameters. 83 00:07:48,550 --> 00:07:50,620 It's not possible using Funk's. 84 00:07:53,240 --> 00:07:59,900 As you can see, having an outer perimeter doesn't work with funk, but it works fine with custom delegate 85 00:08:00,320 --> 00:08:05,540 S.M.A.R.T funks countless optional parameters while custom delegates can. 86 00:08:10,280 --> 00:08:12,440 Again, funk doesn't compile. 87 00:08:12,680 --> 00:08:14,210 But, Doug, it works fine. 88 00:08:14,870 --> 00:08:18,490 Also, we can't have funks with the params parameters. 89 00:08:25,890 --> 00:08:30,720 As you can see, sometimes a custom delegate is simply the only choice we have. 90 00:08:31,470 --> 00:08:38,970 Let's summarize a delegate is a type whose instances hold a reference to a method with particular parameters 91 00:08:38,970 --> 00:08:41,320 and return type in C-sharp. 92 00:08:41,340 --> 00:08:47,790 We can use the predefined generic delegates funk and action to represent any function we want. 93 00:08:48,360 --> 00:08:52,950 During the interview, you might be asked What is a multi-course delegate? 94 00:08:53,250 --> 00:08:58,800 A multi custom delegate is a delegate holding references to more than one function. 95 00:08:59,640 --> 00:09:01,140 All right, that's it. 96 00:09:01,140 --> 00:09:07,560 For this lecture, we will revisit the topic of delegates in the lecture about events as using their 97 00:09:07,560 --> 00:09:09,960 gates is crucial when working with them. 98 00:09:10,440 --> 00:09:14,040 Thanks for watching this video, and I'll see you in the next one.