1 00:00:01,100 --> 00:00:07,130 ‫Come back in this lecture, we will create our own delegates to filter a list of people, and this will 2 00:00:07,130 --> 00:00:11,080 ‫give us a deeper understanding of how delegates work under the hood. 3 00:00:11,090 --> 00:00:14,960 ‫So let's consider the following class that we have here. 4 00:00:14,960 --> 00:00:19,970 ‫So it's a very simple class person with a name property as well as an age property. 5 00:00:19,970 --> 00:00:21,410 ‫Just two properties here. 6 00:00:22,770 --> 00:00:27,990 ‫So we want to create our own delegates to filter people based on the age. 7 00:00:28,350 --> 00:00:36,600 ‫Our filter delegates will take a person as a parameter and we'll return a bool if the condition regarding 8 00:00:36,600 --> 00:00:37,680 ‫the age is met. 9 00:00:38,010 --> 00:00:39,840 ‫What is the condition exactly? 10 00:00:39,870 --> 00:00:46,560 ‫Well, we don't care, since the code could be used to filter a list on a web page, for example, and 11 00:00:46,560 --> 00:00:49,210 ‫the filtration feature might change rapidly. 12 00:00:49,230 --> 00:00:52,490 ‫And, well, that's exactly the beauty of delegates. 13 00:00:52,500 --> 00:00:53,700 ‫What a luxury. 14 00:00:54,000 --> 00:00:54,450 ‫All right. 15 00:00:54,450 --> 00:00:56,880 ‫So let's define our delegate type. 16 00:00:56,970 --> 00:01:04,840 ‫And therefore, I'm going to just go ahead and say that this is going to be a delegate of type Boolean. 17 00:01:04,860 --> 00:01:06,900 ‫I'm going to call it filter delegate. 18 00:01:06,900 --> 00:01:09,390 ‫And it will require a person. 19 00:01:09,930 --> 00:01:12,210 ‫So a person object called P. 20 00:01:13,950 --> 00:01:20,890 ‫Now let's go ahead and just create a little list of people, of person objects, so to speak. 21 00:01:20,910 --> 00:01:23,790 ‫Here we have four person objects. 22 00:01:24,330 --> 00:01:26,610 ‫P one, p two, p three and P four. 23 00:01:26,610 --> 00:01:29,010 ‫And then we add all of them to a list. 24 00:01:29,010 --> 00:01:32,280 ‫So they have different names as well as different ages here. 25 00:01:34,150 --> 00:01:39,130 ‫So now we want to actually filter those people based on their age. 26 00:01:39,130 --> 00:01:48,040 ‫For example, display the adults only so age 18 and higher or the minors only so age below 18. 27 00:01:48,220 --> 00:01:54,160 ‫Since we want to apply different filters, let's actually create a method called display people. 28 00:01:54,250 --> 00:02:01,000 ‫This method will take the list of people and a filter as a delegate, and then it will filter the list 29 00:02:01,000 --> 00:02:02,950 ‫of people based on that filter. 30 00:02:03,250 --> 00:02:11,690 ‫So we also want to throw in a string called title to make things look more organized in the console. 31 00:02:11,710 --> 00:02:16,930 ‫So first of all, let me set up the header of my method here. 32 00:02:17,500 --> 00:02:20,860 ‫So here we have the display people method. 33 00:02:20,980 --> 00:02:27,160 ‫It will require a string, the list of people and the filter delegate that I will call filter. 34 00:02:27,520 --> 00:02:30,160 ‫Now, what is it that I want to do in here? 35 00:02:30,430 --> 00:02:34,000 ‫Well, first of all, I'm just going to write the title. 36 00:02:35,890 --> 00:02:42,160 ‫And then I'm going to go through all the people that are inside of my personal list here. 37 00:02:43,230 --> 00:02:47,940 ‫And I'm going to check if the filter works with the person that we passed. 38 00:02:48,150 --> 00:02:53,730 ‫So here we're just going to say the person's name as well as how old they are. 39 00:02:54,620 --> 00:03:00,080 ‫And now we can go ahead and just define a couple of methods that we will use as filters. 40 00:03:00,830 --> 00:03:03,920 ‫So one will be for the miners. 41 00:03:04,370 --> 00:03:06,320 ‫So here is miner. 42 00:03:06,320 --> 00:03:11,960 ‫So we take in the person and we check their age against 18. 43 00:03:11,960 --> 00:03:15,590 ‫And if it's lower, then we know that we need to return. 44 00:03:15,830 --> 00:03:18,500 ‫True, which means the person is a miner. 45 00:03:18,740 --> 00:03:21,080 ‫Otherwise we will return false. 46 00:03:22,010 --> 00:03:24,840 ‫So that's going to be the one filter. 47 00:03:24,860 --> 00:03:29,270 ‫Then let's create another filter for adults like so. 48 00:03:29,510 --> 00:03:35,660 ‫And now another filter for seniors, let's say people above the age of 65. 49 00:03:36,480 --> 00:03:36,810 ‫Like. 50 00:03:36,810 --> 00:03:43,350 ‫So now on the main method, let's call the display people method a few times, the one that we just 51 00:03:43,350 --> 00:03:44,850 ‫created this one here. 52 00:03:45,330 --> 00:03:48,600 ‫So let's go ahead into our main method. 53 00:03:48,990 --> 00:03:55,380 ‫And here I'm going to display people and here I'm just going to pass in the title. 54 00:03:55,380 --> 00:04:02,610 ‫I'm just going to say kids, for example, I'm passing in the people list as well as my filter. 55 00:04:02,610 --> 00:04:08,430 ‫So here my is minor filter and I call it is minor like so. 56 00:04:10,270 --> 00:04:12,850 ‫And now let's check it out. 57 00:04:15,440 --> 00:04:18,520 ‫Let's run this and see if it will display our kids. 58 00:04:18,530 --> 00:04:21,860 ‫And it only displays Walter, who is 12 years old. 59 00:04:22,610 --> 00:04:24,170 ‫Okay, so that filter worked. 60 00:04:24,260 --> 00:04:30,440 ‫You see, we passed in a method we didn't have to pass in any parameters to this method because we passed 61 00:04:30,440 --> 00:04:32,780 ‫it in the position of a delegate. 62 00:04:32,780 --> 00:04:38,000 ‫So here, this filter delegate, let me scroll out a little so you can see it's a little better. 63 00:04:38,000 --> 00:04:43,490 ‫So here we need to pass into filter delegate, which means we can pass in a method that just follows 64 00:04:43,490 --> 00:04:49,940 ‫the filter delegates description, which means it just needs to pass an ebullient and it needs to have 65 00:04:49,940 --> 00:04:52,130 ‫one parameter of type person. 66 00:04:52,670 --> 00:04:58,610 ‫And that's exactly what our methods slash filters, how I call them here are doing. 67 00:04:58,610 --> 00:05:02,270 ‫They return a boolean and they require person P. 68 00:05:02,360 --> 00:05:07,160 ‫So if it's minor weren't following the structure, this wouldn't work. 69 00:05:07,160 --> 00:05:16,370 ‫So if I were to get rid of this and return true here, for example, then this wouldn't work. 70 00:05:16,400 --> 00:05:22,880 ‫You see here it says is minor well can convert method group to program dot filter delegate so it doesn't 71 00:05:22,880 --> 00:05:25,910 ‫follow the instructions of our delegates. 72 00:05:27,970 --> 00:05:28,570 ‫All right. 73 00:05:28,960 --> 00:05:35,490 ‫So now let's do the same thing with our other type of filters. 74 00:05:35,500 --> 00:05:39,250 ‫So first of all, with adults and then with seniors. 75 00:05:39,250 --> 00:05:40,690 ‫So let's look at it. 76 00:05:41,020 --> 00:05:47,440 ‫And we see kids, Walter, adults, Aiden and Anatoly, and then seniors only Siv. 77 00:05:47,440 --> 00:05:49,150 ‫And he's 69 years old. 78 00:05:49,840 --> 00:05:52,330 ‫So that's it for a little introduction for delegates. 79 00:05:52,330 --> 00:06:00,880 ‫But you might notice a annoying pattern that we have here, and we are going to eliminate that in the 80 00:06:00,880 --> 00:06:05,050 ‫next lecture, which is the anonymous methods pattern. 81 00:06:05,050 --> 00:06:06,400 ‫So see you there.