Welcome back.
In this video we're going to look at lambda expressions.
And then that helps us to understand predicates which then helps us to understand how to use the find
all or any or.
There are so many other methods that are applicable onto lists in C sharp where you need to pass in
lambdas.
So it's really important to understand that.
So a lambda expression consists of two parts.
Number one is the parameters and number two is the expression or statement block.
And on the left hand side are the parameters.
And then on the left hand side of this sign here.
So equal greater than.
By the way this symbol is read as goes to or becomes.
So the expression or action to perform is on the right hand side.
So here we have this example x equal greater sign x multiplied with x semicolon.
And this reads as.
Take an input x and turn it into x multiplied by x okay.
So x goes to or becomes x times x.
So if we took this very simple line and we wanted to make a method out of it, it would look like this.
We're calling it squaring.
We're taking one parameter and we're just saying return the parameter times the parameter.
So x times x in this case right.
Or num one times nine one in this case.
So we could have called it x as well.
But in the end it doesn't matter too much.
But the idea here is that this is the same as this, at least in when you execute it straight away.
So a lambda expression is just a very short way to create a method that is never going to be called
outside of the moment when it is used, because we cannot use this lambda or give it a name and then
use it later, because if we wanted to use it multiple times, we would have to use a method for that.
But the lambda allows us to get the results very quickly without having to create a separate method
that will only be called once anyways, for example, and we can just have the logic very quickly and
very precisely in just one short line of code.
So sometimes you just don't need to write a separate method, and the lambda expression is good enough.
And it is very easy to read to a degree because you can see what the code does straight away.
So if you obviously have a very complex method that you want to execute, a lambda expression is not
the right way to go.
But in this case, it's a very simple expression that we want to achieve.
We just want to achieve that whatever input we give it, we want it to give us the multiplication of
the two.
Now how can we apply this principle onto our lists?
Well, we already did.
So what we said here is we said x becomes x greater equal ten.
So whatever the result of this code will be, whatever this will return, that's what X will become.
So now you might say well this is a little confusing then as we have equal greater than sign and then
we have greater than sign equal.
Well the thing is this here is a lambda.
You can see when you hover over it.
This is a lambda expression.
So this is the key part of a lambda expression.
You need to have equal greater than sign for a lambda expression.
However if you want to use greater equal sign that is a Boolean operation.
So you see here it compares that.
So if you wanted to do less than equal you can do that like this.
But you can never say equal less than or equal greater than okay.
So that won't work.
So that will always be a lambda expression here as you see equal greater than sign.
This is in C sharp defined or in most programming languages defined as the two characters that indicate
that we're talking about a lambda expression here.
So how could we have stored whatever that here is inside of a variable?
Therefore, we could have used a predicate.
And I talked about this keyword earlier.
Right.
So what we can do is we can go ahead and use the keyword predicate.
You can see it's here.
This predicate will allow us well we need to define of what type it is.
This will allow us to store whether something is greater or not.
So we can just say is greater than ten for example.
And then we can assign a lambda expression to it.
So here I can just go ahead and assign this particular lambda expression to it like so.
So what this will do is it will define the predicate to check if a number is greater than ten, like
so.
So the predicate of type int is greater than ten will hold whatever the result of this here is.
And it just says here once you hover over it we're.
Represents the method that defines a set of criteria and determines whether the specified object meets
those criteria, and it returns true if object meets the criteria defined within the method represented
by this delegate and otherwise false.
So whatever we passed in here, it will store that inside of that is greater than predicate.
And then we can use that is greater than predicate inside of our find all method.
So now we're bringing those two concepts together where we're using a lambda expression to store it
inside of a predicate okay.
And like this.
You can read the entire thing where it says represented by this delegate, otherwise false.
Now it would help to understand what a delegate is, but for now I have to say we're going to cover
that later on.
So please be patient with that particular aspect of the C programming language.
You see, we're already diving quite deep into the language, and this will make you really ready for
anything that can come your way.
By the way, here I'm saying is greater than or equal.
Okay.
So now the alternative would be to just get rid of this equal sign.
So only numbers greater than ten will now be part of it okay.
Now this list here higher equal ten will now be called higher ten okay.
So I'm just going to use it like this.
And now what we're going to have is all the numbers that are higher than 1010 excluded okay.
So if you run this you will see all numbers ten and higher in our list 15, 25 and 18 okay.
So now a lot came together.
We had our predicate and we had a lambda expression.
Now this is a little tough to manage.
So let's go ahead and have a few more examples in the next few videos.