‫Come back in this lecture, we will create our own delegates to filter a list of people, and this will
‫give us a deeper understanding of how delegates work under the hood.
‫So let's consider the following class that we have here.
‫So it's a very simple class person with a name property as well as an age property.
‫Just two properties here.
‫So we want to create our own delegates to filter people based on the age.
‫Our filter delegates will take a person as a parameter and we'll return a bool if the condition regarding
‫the age is met.
‫What is the condition exactly?
‫Well, we don't care, since the code could be used to filter a list on a web page, for example, and
‫the filtration feature might change rapidly.
‫And, well, that's exactly the beauty of delegates.
‫What a luxury.
‫All right.
‫So let's define our delegate type.
‫And therefore, I'm going to just go ahead and say that this is going to be a delegate of type Boolean.
‫I'm going to call it filter delegate.
‫And it will require a person.
‫So a person object called P.
‫Now let's go ahead and just create a little list of people, of person objects, so to speak.
‫Here we have four person objects.
‫P one, p two, p three and P four.
‫And then we add all of them to a list.
‫So they have different names as well as different ages here.
‫So now we want to actually filter those people based on their age.
‫For example, display the adults only so age 18 and higher or the minors only so age below 18.
‫Since we want to apply different filters, let's actually create a method called display people.
‫This method will take the list of people and a filter as a delegate, and then it will filter the list
‫of people based on that filter.
‫So we also want to throw in a string called title to make things look more organized in the console.
‫So first of all, let me set up the header of my method here.
‫So here we have the display people method.
‫It will require a string, the list of people and the filter delegate that I will call filter.
‫Now, what is it that I want to do in here?
‫Well, first of all, I'm just going to write the title.
‫And then I'm going to go through all the people that are inside of my personal list here.
‫And I'm going to check if the filter works with the person that we passed.
‫So here we're just going to say the person's name as well as how old they are.
‫And now we can go ahead and just define a couple of methods that we will use as filters.
‫So one will be for the miners.
‫So here is miner.
‫So we take in the person and we check their age against 18.
‫And if it's lower, then we know that we need to return.
‫True, which means the person is a miner.
‫Otherwise we will return false.
‫So that's going to be the one filter.
‫Then let's create another filter for adults like so.
‫And now another filter for seniors, let's say people above the age of 65.
‫Like.
‫So now on the main method, let's call the display people method a few times, the one that we just
‫created this one here.
‫So let's go ahead into our main method.
‫And here I'm going to display people and here I'm just going to pass in the title.
‫I'm just going to say kids, for example, I'm passing in the people list as well as my filter.
‫So here my is minor filter and I call it is minor like so.
‫And now let's check it out.
‫Let's run this and see if it will display our kids.
‫And it only displays Walter, who is 12 years old.
‫Okay, so that filter worked.
‫You see, we passed in a method we didn't have to pass in any parameters to this method because we passed
‫it in the position of a delegate.
‫So here, this filter delegate, let me scroll out a little so you can see it's a little better.
‫So here we need to pass into filter delegate, which means we can pass in a method that just follows
‫the filter delegates description, which means it just needs to pass an ebullient and it needs to have
‫one parameter of type person.
‫And that's exactly what our methods slash filters, how I call them here are doing.
‫They return a boolean and they require person P.
‫So if it's minor weren't following the structure, this wouldn't work.
‫So if I were to get rid of this and return true here, for example, then this wouldn't work.
‫You see here it says is minor well can convert method group to program dot filter delegate so it doesn't
‫follow the instructions of our delegates.
‫All right.
‫So now let's do the same thing with our other type of filters.
‫So first of all, with adults and then with seniors.
‫So let's look at it.
‫And we see kids, Walter, adults, Aiden and Anatoly, and then seniors only Siv.
‫And he's 69 years old.
‫So that's it for a little introduction for delegates.
‫But you might notice a annoying pattern that we have here, and we are going to eliminate that in the
‫next lecture, which is the anonymous methods pattern.
‫So see you there.