1 00:00:01,070 --> 00:00:01,850 ‫Welcome back. 2 00:00:01,880 --> 00:00:04,730 ‫In this lecture, you will learn about anonymous methods. 3 00:00:04,730 --> 00:00:07,550 ‫An anonymous method is a method without a name. 4 00:00:07,550 --> 00:00:08,510 ‫Well, who gets it? 5 00:00:08,510 --> 00:00:14,450 ‫And anonymous methods in C sharp can be defined using the delegate keyword and can be assigned to a 6 00:00:14,450 --> 00:00:16,380 ‫variable of the delegate type. 7 00:00:16,400 --> 00:00:22,820 ‫This means we don't have to define a method of our own each time we want to call a method that needs 8 00:00:22,820 --> 00:00:25,820 ‫a delegate, since maybe we want to call it once. 9 00:00:25,820 --> 00:00:32,360 ‫So defining a dedicated delegate every time we need one can ruin the code structure very fast. 10 00:00:32,360 --> 00:00:39,710 ‫So adding to our previous example, where we created a bunch of filters, you might recall that here 11 00:00:39,950 --> 00:00:43,010 ‫is adult, is senior and all of the good stuff. 12 00:00:43,460 --> 00:00:49,730 ‫And now let's go ahead and create two additional filters where we create a custom Boolean expression 13 00:00:49,730 --> 00:00:56,030 ‫as our filter and a second filter where we simply display all the entries in our list. 14 00:00:56,420 --> 00:01:01,850 ‫So let's go ahead and go to our main method where we displayed all of the people, right? 15 00:01:01,850 --> 00:01:07,790 ‫We call this method here and now let's create a new filter delegate. 16 00:01:07,790 --> 00:01:14,990 ‫So I'm just going to use the filter delegate keyword, the one that we have set up here at the top. 17 00:01:14,990 --> 00:01:23,150 ‫And you will see that it needs to return a boolean and it needs to have a person as the parameter. 18 00:01:23,420 --> 00:01:25,340 ‫So let's call it filter. 19 00:01:25,340 --> 00:01:34,400 ‫And here we need to use the delegate keyword once again and then we can use the parameters that we need 20 00:01:34,400 --> 00:01:35,150 ‫to pass. 21 00:01:35,660 --> 00:01:41,600 ‫And you can see it's still complaining because first of all, we need to add a semicolon here to bottom 22 00:01:41,600 --> 00:01:46,100 ‫and then we need to make sure that we return a value. 23 00:01:47,000 --> 00:01:48,680 ‫Of type boolean. 24 00:01:48,680 --> 00:01:52,400 ‫So not all code paths return a value in anonymous method. 25 00:01:52,460 --> 00:01:55,250 ‫So it already says anonymous method here. 26 00:01:55,250 --> 00:02:04,970 ‫So what we're just going to say is just return the H where it's greater or equal to 20 and where the 27 00:02:04,970 --> 00:02:07,880 ‫H is less than 30. 28 00:02:08,580 --> 00:02:09,720 ‫And equal 30. 29 00:02:10,260 --> 00:02:10,660 ‫All right. 30 00:02:10,680 --> 00:02:11,770 ‫That's pretty much it. 31 00:02:11,790 --> 00:02:16,020 ‫So here we create a new variable called filter of type filter delegate. 32 00:02:16,020 --> 00:02:17,760 ‫So this is just a variable, right? 33 00:02:18,150 --> 00:02:24,000 ‫Then we assign an anonymous method to it instead of an already defined method. 34 00:02:24,300 --> 00:02:29,520 ‫And then we just make sure that we follow the instructions of that filter delegate, and that is to 35 00:02:29,520 --> 00:02:30,750 ‫return a boolean. 36 00:02:30,960 --> 00:02:32,760 ‫So that's exactly what we're doing. 37 00:02:32,760 --> 00:02:36,960 ‫We're returning true if those two statements are true. 38 00:02:36,960 --> 00:02:41,850 ‫So if the age is greater or equal, 20 or less and equal 30. 39 00:02:42,090 --> 00:02:44,700 ‫So don't forget that semicolon at the end. 40 00:02:44,700 --> 00:02:46,140 ‫That's really important here. 41 00:02:46,170 --> 00:02:51,000 ‫Otherwise this won't work because this is basically like creating a variable. 42 00:02:52,440 --> 00:02:56,010 ‫So it's like int x equals three. 43 00:02:56,040 --> 00:03:00,960 ‫It's pretty much the same thing that we're doing here with this filter delegates, but it's a little 44 00:03:00,960 --> 00:03:02,520 ‫more complicated. 45 00:03:02,550 --> 00:03:08,970 ‫So this year what you see here is an anonymous method because it doesn't have a name, but it basically 46 00:03:08,970 --> 00:03:10,140 ‫behaves like a method. 47 00:03:11,180 --> 00:03:15,050 ‫So now what we can do is we can call our display people. 48 00:03:15,910 --> 00:03:20,170 ‫And we can use our variable filter so we can pass a filter. 49 00:03:20,200 --> 00:03:26,740 ‫You might recall this play, people always had this structure where you had a title right here you had 50 00:03:26,740 --> 00:03:34,300 ‫a title, then you had to pass in the list of people so of person objects, right? 51 00:03:34,540 --> 00:03:38,050 ‫And then you needed to add the filter in here. 52 00:03:38,050 --> 00:03:43,540 ‫And I'm just going to use my filter that I had just set up, which is this filter variable. 53 00:03:43,540 --> 00:03:49,360 ‫So now this filter variable is off type filter delegate, which is what display people requires because 54 00:03:49,360 --> 00:03:53,410 ‫here you see it requires a filter delegate type. 55 00:03:53,680 --> 00:04:00,280 ‫So it's really something interesting because you can on one hand just go ahead and create a method that 56 00:04:00,280 --> 00:04:05,140 ‫follows the delegate description and just use it as we saw here. 57 00:04:05,140 --> 00:04:11,860 ‫But you can also just create a variable which uses an anonymous method using the delegate keyword, 58 00:04:11,860 --> 00:04:16,240 ‫as we have done here, and install it inside of this filter and then use that filter here. 59 00:04:16,240 --> 00:04:21,340 ‫So this will now, of course, not be kids, but it will be between 20 and 30 and it will display all 60 00:04:21,340 --> 00:04:23,320 ‫the people that we have there. 61 00:04:23,320 --> 00:04:25,810 ‫So let's run this code and see if that works. 62 00:04:25,810 --> 00:04:32,290 ‫And we see here between 20 and 30, we have Anatoli, who is 25 years old, but no one else. 63 00:04:32,530 --> 00:04:40,600 ‫So now another approach of an anonymous method, and that is that we can pass an anonymous method directly 64 00:04:40,600 --> 00:04:41,890 ‫as a parameter. 65 00:04:42,460 --> 00:04:43,930 ‫So let's do that real quick. 66 00:04:43,930 --> 00:04:49,690 ‫So we call this display people method and this time I'm just going to display all people, right? 67 00:04:49,690 --> 00:04:51,370 ‫So here all people. 68 00:04:51,370 --> 00:04:56,200 ‫But now we need to pass in, you see here a filter delegate. 69 00:04:56,230 --> 00:04:58,960 ‫So let's go ahead and create an anonymous method. 70 00:04:58,960 --> 00:05:02,530 ‫And in order to do that, we need to use the delegate keyword, right? 71 00:05:02,530 --> 00:05:08,740 ‫They'll get like so and we need to of course still follow the same instruction. 72 00:05:08,740 --> 00:05:16,180 ‫We still need to make sure that our delegate that we're passing is going to be of type filter delegate 73 00:05:16,180 --> 00:05:19,090 ‫if we look at it here at the top. 74 00:05:19,090 --> 00:05:23,500 ‫So here this filter delegate needed to return a pool and it needed a person. 75 00:05:23,500 --> 00:05:32,500 ‫So let's get in the person as the parameter P But then of course, as I stated, we need to also return 76 00:05:33,820 --> 00:05:35,530 ‫something like a boolean. 77 00:05:35,770 --> 00:05:39,280 ‫So basically we can just call it like so. 78 00:05:39,550 --> 00:05:42,550 ‫So here this is a very condensed line of code. 79 00:05:42,550 --> 00:05:43,930 ‫There is a lot going on here. 80 00:05:43,930 --> 00:05:49,870 ‫We're displaying people, but we also have a filter here and this filter, it takes all the people and 81 00:05:49,870 --> 00:05:55,150 ‫it just returns true because it really doesn't matter that much which person we put in here. 82 00:05:55,150 --> 00:05:56,980 ‫So that's basically it. 83 00:05:56,980 --> 00:06:02,110 ‫Otherwise, of course you can also write it like so this will maybe be a little more readable. 84 00:06:02,350 --> 00:06:11,680 ‫So this here is our anonymous method that we are passing as the third parameter as we are just following 85 00:06:11,680 --> 00:06:13,000 ‫the filter delegate. 86 00:06:14,430 --> 00:06:15,170 ‫Set up. 87 00:06:15,180 --> 00:06:20,550 ‫So you see here we're returning a bull and we are taking in a person as the parameter. 88 00:06:20,550 --> 00:06:25,710 ‫So now you see the power of delegates because they are super flexible. 89 00:06:25,710 --> 00:06:30,390 ‫They really just want you to follow a certain structure. 90 00:06:30,510 --> 00:06:31,860 ‫The rest is up to you. 91 00:06:31,860 --> 00:06:36,390 ‫And this gives you as a developer a lot of flexibility when working with delegates. 92 00:06:37,080 --> 00:06:37,470 ‫All right. 93 00:06:37,470 --> 00:06:41,520 ‫So that was a little introduction into anonymous methods. 94 00:06:41,550 --> 00:06:44,220 ‫Let's next look at Lambda Expressions.