‫All right.
‫So now that, you know, the general idea of delegates, let's look at them in action.
‫And to understand them better, we will start by using a delegate that already exists in C-sharp.
‫So, first of all, let's start by defining a simple list of strings called names.
‫And then I'm going to use this.
‫Let's say I want to remove names from it.
‫So here there is this remove all method and this remove all method allows me to remove all the elements
‫that match the conditions defined by the specified predicate.
‫So here you see this green predicate keyword.
‫Here is basically what we can pass.
‫So this is basically here I delegates that is expected predicate is just the name of the delegate that
‫is expected.
‫Once you click on it, you will find it here.
‫Public delegate pool predicate.
‫So here we can see that the predicate is a delegate that takes an object of RT, which means specific
‫type.
‫In this case, RT is the type of our list elements.
‫So in our case it's a string and it returns a boolean.
‫So when we call the remove all method, this predicate will be called on every name in our list.
‫If we read the documentation, we will learn that the bool that is returned by this predicate will determine
‫whether or not this element will be removed from the list.
‫If it returns true, then the element will be removed.
‫And it is our job to write these rules to determine the fate of our names in our list.
‫So of these names that we have here, which means Aidan's if folder and Anatoly.
‫After all, the people who wrote the code behind the remove all method can't read our minds to determine
‫the rules we want.
‫Exactly.
‫That's why we will create our own method that matches the definition of the predicate delegate and pass
‫it to the remove all method.
‫Let's say we want to remove all the names that contain the letter I so we can write our filtration method
‫like so.
‫So we just create a static pool filter which will take in a string.
‫So this is our method called filter, and we return whether the string contains the letter I.
‫And here we're using the contains method which will return a boolean which will return true or false.
‫So here the contains the C, it takes a string checks if it contains that, and then it returns a boolean.
‫So what we do here is we in fact return a boolean.
‫And it takes a string as a parameter.
‫So basically we will return.
‫True if the string contains I and then the remove all method will execute this filter method on all
‫the elements in our list.
‫So let's now pass our filter method to the remove all method that we have up here that isn't finished
‫yet and still needs a delegate that we can pass to it.
‫And let's add a for each loop before and after the remove all method just for observation.
‫So I'm going to add this little code in here where I just say this is what happened before.
‫So we write a list of names onto the console with all the names that are in our list here.
‫And then we remove all that, follow a certain filter, and we run it once again.
‫Now this filter that we're passing, you see, we don't use the brackets here, even though it's a method.
‫So basically we're passing a method as a parameter here and we're checking if the string that we're
‫passing contains an AI.
‫So let's run it and see which names contain an I.
‫So we have agents if Walter and Anatoly.
‫And after that, we removed all except for Walter, because Walter is a string that doesn't contain
‫an eye.
‫And that's basically how we have used a delegate that was pre-existing.
‫And now we have used a method that requires a delegate that we can pass to it.
‫So basically what happened is that names this list of strings here.
‫Was given to remove all and checked one by one with the filter that we have used here.
‫So it passed every single value from names one after the other into our filter method, ran through
‫that filter and returned if it should delete it or not.
‫So this boolean will be true if I is contained in the string that was passed.
‫So the string that we're currently looking at in our names list and if this here returns true, then
‫the filter method will return true, which will then lead to remove all taking care of removing the
‫name from the names list.
‫All right, so this was just one example of a delegate.
‫And you see, we didn't pass specifically here what we want to have.
‫So we didn't say which string we want to take, for example, by saying name starts and then index or
‫whatever.
‫We didn't have to do any of that because well remove all expects a delegate here and that's what we
‫passed to it.
‫If this is still a little confusing, no worries.
‫We're going to have a couple more examples which will definitely help you to understand delegates a
‫little better, because we're going to create some ourselves and then it will make a lot more sense.
‫So see you in the next video.