1 00:00:01,420 --> 00:00:01,720 ‫All right. 2 00:00:01,720 --> 00:00:07,870 ‫So we saw in the previous lecture how anonymous methods can help us to write the code blocks in line 3 00:00:07,870 --> 00:00:09,490 ‫where delegates are required. 4 00:00:09,670 --> 00:00:13,390 ‫In C sharp 3.0 lambda expressions were introduced. 5 00:00:13,480 --> 00:00:19,060 ‫They provide a simple and more compact, functional syntax to write anonymous methods. 6 00:00:19,810 --> 00:00:25,840 ‫The word lambda is taken from the lambda calculus where everything is expressed in terms of functions. 7 00:00:25,840 --> 00:00:30,070 ‫We will be using lambda expressions to create anonymous functions and methods. 8 00:00:30,070 --> 00:00:38,020 ‫To create a lambda expression, we need to use the Lambda Declaration operator, which is equal greater 9 00:00:38,470 --> 00:00:45,670 ‫also read as goes into or goes to to separate the lambda parameter list from its body. 10 00:00:45,790 --> 00:00:49,120 ‫Lambda Expression can have one of the following two forms. 11 00:00:49,240 --> 00:00:56,530 ‫So first of all, an expression lambda that has an expression one line of code as its body. 12 00:00:56,680 --> 00:01:00,760 ‫So input parameters equal greater than expression. 13 00:01:01,360 --> 00:01:04,750 ‫And then we have the statement, lambda that has a statement. 14 00:01:04,750 --> 00:01:09,580 ‫BLOCK Executing more than one line of code as its body. 15 00:01:10,030 --> 00:01:17,440 ‫And here we just use the input parameters equal, greater, and then the sequence of statements inside 16 00:01:17,440 --> 00:01:18,820 ‫of the curly brackets. 17 00:01:19,180 --> 00:01:21,910 ‫So now let's have a look at that in practice. 18 00:01:23,920 --> 00:01:29,970 ‫So now going back to our previous example, let's add a few filters using lambda expressions. 19 00:01:30,120 --> 00:01:38,400 ‫So here we had the filter where we showed all people right now let's go ahead and create a search keyword 20 00:01:39,600 --> 00:01:42,400 ‫and I'm just going to call this one a okay? 21 00:01:42,480 --> 00:01:48,510 ‫So I'm just going to call more search for a letter and then I'm going to call the Display People method 22 00:01:48,510 --> 00:01:49,320 ‫once again. 23 00:01:49,320 --> 00:01:56,610 ‫And I'm going to just going to say I'm going to display everything where the H is above 20 with a search 24 00:01:56,610 --> 00:01:57,240 ‫keyword. 25 00:01:57,240 --> 00:02:03,480 ‫So that means we're looking for anyone who who's older than 20 but also has, for example, an A in 26 00:02:03,480 --> 00:02:04,260 ‫their name. 27 00:02:04,890 --> 00:02:11,580 ‫So let's go ahead and do that by also adding this search keyword to this statement here. 28 00:02:11,850 --> 00:02:13,650 ‫So this is basically just the title. 29 00:02:13,660 --> 00:02:13,870 ‫Okay? 30 00:02:13,950 --> 00:02:21,120 ‫This is the title of our display people method, the string here and now comes the list of people that 31 00:02:21,120 --> 00:02:25,050 ‫we want to iterate through or check to filter, so to speak. 32 00:02:25,050 --> 00:02:34,050 ‫So now let's just pass in the people list that we created earlier, which is this list up here, list 33 00:02:34,050 --> 00:02:37,950 ‫of person people, and we have all four people in there. 34 00:02:39,360 --> 00:02:45,750 ‫Now that we have people, let's go ahead and actually use our lambda expression. 35 00:02:45,750 --> 00:02:54,690 ‫So I'm going to pass in the parameter P and then I'm going to run the following expression in it like. 36 00:02:54,690 --> 00:03:00,450 ‫So of course I need to finish this statement with an exclamation mark, but now let's actually execute 37 00:03:00,450 --> 00:03:01,170 ‫some code. 38 00:03:01,170 --> 00:03:07,980 ‫So I'm using a lambda expression here instead of an anonymous method, instead of passing a method so 39 00:03:07,980 --> 00:03:11,010 ‫here, which is saying these are all the parameters. 40 00:03:11,940 --> 00:03:16,830 ‫Equal greater then and this is just one parameter, which is why it's fine to not use brackets. 41 00:03:16,830 --> 00:03:20,790 ‫Otherwise I would have to surround it with brackets here like so. 42 00:03:21,060 --> 00:03:26,520 ‫And then I can go ahead and do what I want to execute. 43 00:03:26,520 --> 00:03:28,350 ‫And what is it that I'm going to execute? 44 00:03:28,350 --> 00:03:35,580 ‫Well, I'm just going to check if the name contains a certain keyword, which is our search keyword. 45 00:03:35,580 --> 00:03:43,920 ‫So I'm just going to say if name contains the search keyword and the age is greater than 20, so the 46 00:03:43,920 --> 00:03:51,150 ‫person's h the individual person that we're currently checking, if that is greater than 20, then just 47 00:03:51,150 --> 00:03:51,990 ‫return true. 48 00:03:51,990 --> 00:03:55,620 ‫Because if we look at it our. 49 00:03:57,030 --> 00:04:02,780 ‫Filter delegates that we are replacing now, or in which position we are now passing. 50 00:04:02,790 --> 00:04:06,690 ‫Our lambda expression is requiring a boolean. 51 00:04:06,990 --> 00:04:10,860 ‫As we have seen here when we first set it up. 52 00:04:11,430 --> 00:04:11,850 ‫All right. 53 00:04:11,850 --> 00:04:15,930 ‫And now we, of course, need also to add an else block here. 54 00:04:15,930 --> 00:04:20,040 ‫So else we're just going to return false. 55 00:04:20,040 --> 00:04:23,790 ‫So return false and that's it. 56 00:04:24,510 --> 00:04:26,880 ‫So now no problems there anymore. 57 00:04:26,880 --> 00:04:35,070 ‫We have basically used our lambda here and this is, by the way, a statement lambda that we're using. 58 00:04:35,070 --> 00:04:37,560 ‫So let me add a little bit of a description here. 59 00:04:37,560 --> 00:04:40,080 ‫Statement lambda, we have our search keyword. 60 00:04:40,080 --> 00:04:43,800 ‫We check if the person contains the search keyword and the H. 61 00:04:44,250 --> 00:04:44,670 ‫All right. 62 00:04:44,670 --> 00:04:47,460 ‫And now let's look at the same thing. 63 00:04:47,880 --> 00:04:50,310 ‫So at the lambda expression. 64 00:04:50,310 --> 00:04:54,600 ‫But now let's look at the expression lambda and not the statement lambda. 65 00:04:54,600 --> 00:05:00,270 ‫So the good thing is this is going to be even shorter in terms of the code. 66 00:05:00,540 --> 00:05:09,150 ‫So let's go ahead and say that we want to display the people who are exactly 25 years old. 67 00:05:09,600 --> 00:05:09,990 ‫All right. 68 00:05:09,990 --> 00:05:11,340 ‫So let's just do that. 69 00:05:11,340 --> 00:05:13,980 ‫We're going to filter the people list. 70 00:05:13,980 --> 00:05:16,980 ‫And here I'm going to use a expression lambda. 71 00:05:16,980 --> 00:05:25,260 ‫So I'm just going to say p equal, greater, where p h equals equals 25 and that's it. 72 00:05:25,560 --> 00:05:27,000 ‫So that's how simple it is. 73 00:05:27,000 --> 00:05:30,240 ‫This is our expression lambda. 74 00:05:30,570 --> 00:05:37,470 ‫And the beauty is it's just one line of code, which is the power of the expression lambdas. 75 00:05:37,980 --> 00:05:44,190 ‫So here, instead of having to create an extra method, instead of having to create a anonymous method, 76 00:05:44,190 --> 00:05:50,190 ‫we just use an expression lambda with just one line of code and we basically achieve the same thing 77 00:05:50,190 --> 00:05:53,010 ‫as we achieved here with our filters pretty much. 78 00:05:53,010 --> 00:05:53,520 ‫Right? 79 00:05:54,030 --> 00:05:57,100 ‫So that's really the beauty of expression lambdas. 80 00:05:57,120 --> 00:06:06,030 ‫And now if we run this, we will get the results here saying, well, Anatoliy is 25 years old and Aiden 81 00:06:06,030 --> 00:06:10,470 ‫is also created in 20 with a search keyword. 82 00:06:10,500 --> 00:06:10,980 ‫Eh? 83 00:06:11,010 --> 00:06:13,170 ‫So these are the two up here. 84 00:06:14,220 --> 00:06:16,330 ‫And then who's exactly 25? 85 00:06:16,350 --> 00:06:18,130 ‫Well, that's only Anatoly. 86 00:06:18,960 --> 00:06:21,390 ‫Och, that's pretty much it. 87 00:06:21,780 --> 00:06:25,110 ‫So I hope you enjoyed this part of the series. 88 00:06:25,140 --> 00:06:28,920 ‫In the next video, we're going to look at events and multicast delegates. 89 00:06:28,920 --> 00:06:30,180 ‫So see you there.