1 00:00:01,140 --> 00:00:01,500 ‫All right. 2 00:00:01,500 --> 00:00:06,180 ‫So now that, you know, the general idea of delegates, let's look at them in action. 3 00:00:06,180 --> 00:00:10,600 ‫And to understand them better, we will start by using a delegate that already exists in C-sharp. 4 00:00:10,620 --> 00:00:14,730 ‫So, first of all, let's start by defining a simple list of strings called names. 5 00:00:14,790 --> 00:00:17,810 ‫And then I'm going to use this. 6 00:00:17,820 --> 00:00:21,810 ‫Let's say I want to remove names from it. 7 00:00:21,810 --> 00:00:28,380 ‫So here there is this remove all method and this remove all method allows me to remove all the elements 8 00:00:28,380 --> 00:00:32,250 ‫that match the conditions defined by the specified predicate. 9 00:00:32,310 --> 00:00:36,060 ‫So here you see this green predicate keyword. 10 00:00:36,060 --> 00:00:39,030 ‫Here is basically what we can pass. 11 00:00:39,450 --> 00:00:46,020 ‫So this is basically here I delegates that is expected predicate is just the name of the delegate that 12 00:00:46,020 --> 00:00:47,040 ‫is expected. 13 00:00:47,160 --> 00:00:49,080 ‫Once you click on it, you will find it here. 14 00:00:49,110 --> 00:00:51,210 ‫Public delegate pool predicate. 15 00:00:51,210 --> 00:00:56,850 ‫So here we can see that the predicate is a delegate that takes an object of RT, which means specific 16 00:00:56,850 --> 00:00:57,440 ‫type. 17 00:00:57,450 --> 00:01:00,360 ‫In this case, RT is the type of our list elements. 18 00:01:00,360 --> 00:01:04,980 ‫So in our case it's a string and it returns a boolean. 19 00:01:05,370 --> 00:01:11,370 ‫So when we call the remove all method, this predicate will be called on every name in our list. 20 00:01:11,400 --> 00:01:17,670 ‫If we read the documentation, we will learn that the bool that is returned by this predicate will determine 21 00:01:17,670 --> 00:01:21,120 ‫whether or not this element will be removed from the list. 22 00:01:21,150 --> 00:01:24,390 ‫If it returns true, then the element will be removed. 23 00:01:24,390 --> 00:01:29,430 ‫And it is our job to write these rules to determine the fate of our names in our list. 24 00:01:29,430 --> 00:01:34,410 ‫So of these names that we have here, which means Aidan's if folder and Anatoly. 25 00:01:37,210 --> 00:01:43,240 ‫After all, the people who wrote the code behind the remove all method can't read our minds to determine 26 00:01:43,240 --> 00:01:44,350 ‫the rules we want. 27 00:01:44,350 --> 00:01:45,190 ‫Exactly. 28 00:01:45,280 --> 00:01:51,700 ‫That's why we will create our own method that matches the definition of the predicate delegate and pass 29 00:01:51,700 --> 00:01:54,670 ‫it to the remove all method. 30 00:01:55,090 --> 00:02:02,440 ‫Let's say we want to remove all the names that contain the letter I so we can write our filtration method 31 00:02:02,440 --> 00:02:03,280 ‫like so. 32 00:02:05,720 --> 00:02:11,030 ‫So we just create a static pool filter which will take in a string. 33 00:02:11,450 --> 00:02:18,590 ‫So this is our method called filter, and we return whether the string contains the letter I. 34 00:02:18,950 --> 00:02:25,240 ‫And here we're using the contains method which will return a boolean which will return true or false. 35 00:02:25,250 --> 00:02:32,900 ‫So here the contains the C, it takes a string checks if it contains that, and then it returns a boolean. 36 00:02:32,900 --> 00:02:36,500 ‫So what we do here is we in fact return a boolean. 37 00:02:38,580 --> 00:02:40,890 ‫And it takes a string as a parameter. 38 00:02:40,890 --> 00:02:42,510 ‫So basically we will return. 39 00:02:42,510 --> 00:02:48,180 ‫True if the string contains I and then the remove all method will execute this filter method on all 40 00:02:48,180 --> 00:02:49,770 ‫the elements in our list. 41 00:02:50,010 --> 00:02:56,310 ‫So let's now pass our filter method to the remove all method that we have up here that isn't finished 42 00:02:56,310 --> 00:02:59,940 ‫yet and still needs a delegate that we can pass to it. 43 00:03:00,000 --> 00:03:06,540 ‫And let's add a for each loop before and after the remove all method just for observation. 44 00:03:06,690 --> 00:03:12,870 ‫So I'm going to add this little code in here where I just say this is what happened before. 45 00:03:12,870 --> 00:03:19,740 ‫So we write a list of names onto the console with all the names that are in our list here. 46 00:03:20,100 --> 00:03:27,000 ‫And then we remove all that, follow a certain filter, and we run it once again. 47 00:03:29,700 --> 00:03:34,980 ‫Now this filter that we're passing, you see, we don't use the brackets here, even though it's a method. 48 00:03:34,980 --> 00:03:41,460 ‫So basically we're passing a method as a parameter here and we're checking if the string that we're 49 00:03:41,460 --> 00:03:43,750 ‫passing contains an AI. 50 00:03:43,800 --> 00:03:47,610 ‫So let's run it and see which names contain an I. 51 00:03:48,300 --> 00:03:51,060 ‫So we have agents if Walter and Anatoly. 52 00:03:51,060 --> 00:03:57,210 ‫And after that, we removed all except for Walter, because Walter is a string that doesn't contain 53 00:03:57,210 --> 00:03:57,960 ‫an eye. 54 00:03:58,830 --> 00:04:04,740 ‫And that's basically how we have used a delegate that was pre-existing. 55 00:04:04,740 --> 00:04:09,090 ‫And now we have used a method that requires a delegate that we can pass to it. 56 00:04:11,410 --> 00:04:16,270 ‫So basically what happened is that names this list of strings here. 57 00:04:17,480 --> 00:04:23,780 ‫Was given to remove all and checked one by one with the filter that we have used here. 58 00:04:23,900 --> 00:04:32,450 ‫So it passed every single value from names one after the other into our filter method, ran through 59 00:04:32,450 --> 00:04:35,480 ‫that filter and returned if it should delete it or not. 60 00:04:35,480 --> 00:04:40,190 ‫So this boolean will be true if I is contained in the string that was passed. 61 00:04:40,190 --> 00:04:48,200 ‫So the string that we're currently looking at in our names list and if this here returns true, then 62 00:04:48,200 --> 00:04:54,740 ‫the filter method will return true, which will then lead to remove all taking care of removing the 63 00:04:54,740 --> 00:04:56,660 ‫name from the names list. 64 00:04:57,320 --> 00:05:01,310 ‫All right, so this was just one example of a delegate. 65 00:05:01,310 --> 00:05:06,920 ‫And you see, we didn't pass specifically here what we want to have. 66 00:05:06,920 --> 00:05:12,680 ‫So we didn't say which string we want to take, for example, by saying name starts and then index or 67 00:05:12,680 --> 00:05:13,190 ‫whatever. 68 00:05:13,190 --> 00:05:20,570 ‫We didn't have to do any of that because well remove all expects a delegate here and that's what we 69 00:05:20,570 --> 00:05:21,440 ‫passed to it. 70 00:05:23,220 --> 00:05:25,800 ‫If this is still a little confusing, no worries. 71 00:05:25,800 --> 00:05:29,730 ‫We're going to have a couple more examples which will definitely help you to understand delegates a 72 00:05:29,730 --> 00:05:34,590 ‫little better, because we're going to create some ourselves and then it will make a lot more sense. 73 00:05:34,590 --> 00:05:36,300 ‫So see you in the next video.