‫All right.
‫So we saw in the previous lecture how anonymous methods can help us to write the code blocks in line
‫where delegates are required.
‫In C sharp 3.0 lambda expressions were introduced.
‫They provide a simple and more compact, functional syntax to write anonymous methods.
‫The word lambda is taken from the lambda calculus where everything is expressed in terms of functions.
‫We will be using lambda expressions to create anonymous functions and methods.
‫To create a lambda expression, we need to use the Lambda Declaration operator, which is equal greater
‫also read as goes into or goes to to separate the lambda parameter list from its body.
‫Lambda Expression can have one of the following two forms.
‫So first of all, an expression lambda that has an expression one line of code as its body.
‫So input parameters equal greater than expression.
‫And then we have the statement, lambda that has a statement.
‫BLOCK Executing more than one line of code as its body.
‫And here we just use the input parameters equal, greater, and then the sequence of statements inside
‫of the curly brackets.
‫So now let's have a look at that in practice.
‫So now going back to our previous example, let's add a few filters using lambda expressions.
‫So here we had the filter where we showed all people right now let's go ahead and create a search keyword
‫and I'm just going to call this one a okay?
‫So I'm just going to call more search for a letter and then I'm going to call the Display People method
‫once again.
‫And I'm going to just going to say I'm going to display everything where the H is above 20 with a search
‫keyword.
‫So that means we're looking for anyone who who's older than 20 but also has, for example, an A in
‫their name.
‫So let's go ahead and do that by also adding this search keyword to this statement here.
‫So this is basically just the title.
‫Okay?
‫This is the title of our display people method, the string here and now comes the list of people that
‫we want to iterate through or check to filter, so to speak.
‫So now let's just pass in the people list that we created earlier, which is this list up here, list
‫of person people, and we have all four people in there.
‫Now that we have people, let's go ahead and actually use our lambda expression.
‫So I'm going to pass in the parameter P and then I'm going to run the following expression in it like.
‫So of course I need to finish this statement with an exclamation mark, but now let's actually execute
‫some code.
‫So I'm using a lambda expression here instead of an anonymous method, instead of passing a method so
‫here, which is saying these are all the parameters.
‫Equal greater then and this is just one parameter, which is why it's fine to not use brackets.
‫Otherwise I would have to surround it with brackets here like so.
‫And then I can go ahead and do what I want to execute.
‫And what is it that I'm going to execute?
‫Well, I'm just going to check if the name contains a certain keyword, which is our search keyword.
‫So I'm just going to say if name contains the search keyword and the age is greater than 20, so the
‫person's h the individual person that we're currently checking, if that is greater than 20, then just
‫return true.
‫Because if we look at it our.
‫Filter delegates that we are replacing now, or in which position we are now passing.
‫Our lambda expression is requiring a boolean.
‫As we have seen here when we first set it up.
‫All right.
‫And now we, of course, need also to add an else block here.
‫So else we're just going to return false.
‫So return false and that's it.
‫So now no problems there anymore.
‫We have basically used our lambda here and this is, by the way, a statement lambda that we're using.
‫So let me add a little bit of a description here.
‫Statement lambda, we have our search keyword.
‫We check if the person contains the search keyword and the H.
‫All right.
‫And now let's look at the same thing.
‫So at the lambda expression.
‫But now let's look at the expression lambda and not the statement lambda.
‫So the good thing is this is going to be even shorter in terms of the code.
‫So let's go ahead and say that we want to display the people who are exactly 25 years old.
‫All right.
‫So let's just do that.
‫We're going to filter the people list.
‫And here I'm going to use a expression lambda.
‫So I'm just going to say p equal, greater, where p h equals equals 25 and that's it.
‫So that's how simple it is.
‫This is our expression lambda.
‫And the beauty is it's just one line of code, which is the power of the expression lambdas.
‫So here, instead of having to create an extra method, instead of having to create a anonymous method,
‫we just use an expression lambda with just one line of code and we basically achieve the same thing
‫as we achieved here with our filters pretty much.
‫Right?
‫So that's really the beauty of expression lambdas.
‫And now if we run this, we will get the results here saying, well, Anatoliy is 25 years old and Aiden
‫is also created in 20 with a search keyword.
‫Eh?
‫So these are the two up here.
‫And then who's exactly 25?
‫Well, that's only Anatoly.
‫Och, that's pretty much it.
‫So I hope you enjoyed this part of the series.
‫In the next video, we're going to look at events and multicast delegates.
‫So see you there.