‫Welcome back.
‫In this lecture, you will learn about anonymous methods.
‫An anonymous method is a method without a name.
‫Well, who gets it?
‫And anonymous methods in C sharp can be defined using the delegate keyword and can be assigned to a
‫variable of the delegate type.
‫This means we don't have to define a method of our own each time we want to call a method that needs
‫a delegate, since maybe we want to call it once.
‫So defining a dedicated delegate every time we need one can ruin the code structure very fast.
‫So adding to our previous example, where we created a bunch of filters, you might recall that here
‫is adult, is senior and all of the good stuff.
‫And now let's go ahead and create two additional filters where we create a custom Boolean expression
‫as our filter and a second filter where we simply display all the entries in our list.
‫So let's go ahead and go to our main method where we displayed all of the people, right?
‫We call this method here and now let's create a new filter delegate.
‫So I'm just going to use the filter delegate keyword, the one that we have set up here at the top.
‫And you will see that it needs to return a boolean and it needs to have a person as the parameter.
‫So let's call it filter.
‫And here we need to use the delegate keyword once again and then we can use the parameters that we need
‫to pass.
‫And you can see it's still complaining because first of all, we need to add a semicolon here to bottom
‫and then we need to make sure that we return a value.
‫Of type boolean.
‫So not all code paths return a value in anonymous method.
‫So it already says anonymous method here.
‫So what we're just going to say is just return the H where it's greater or equal to 20 and where the
‫H is less than 30.
‫And equal 30.
‫All right.
‫That's pretty much it.
‫So here we create a new variable called filter of type filter delegate.
‫So this is just a variable, right?
‫Then we assign an anonymous method to it instead of an already defined method.
‫And then we just make sure that we follow the instructions of that filter delegate, and that is to
‫return a boolean.
‫So that's exactly what we're doing.
‫We're returning true if those two statements are true.
‫So if the age is greater or equal, 20 or less and equal 30.
‫So don't forget that semicolon at the end.
‫That's really important here.
‫Otherwise this won't work because this is basically like creating a variable.
‫So it's like int x equals three.
‫It's pretty much the same thing that we're doing here with this filter delegates, but it's a little
‫more complicated.
‫So this year what you see here is an anonymous method because it doesn't have a name, but it basically
‫behaves like a method.
‫So now what we can do is we can call our display people.
‫And we can use our variable filter so we can pass a filter.
‫You might recall this play, people always had this structure where you had a title right here you had
‫a title, then you had to pass in the list of people so of person objects, right?
‫And then you needed to add the filter in here.
‫And I'm just going to use my filter that I had just set up, which is this filter variable.
‫So now this filter variable is off type filter delegate, which is what display people requires because
‫here you see it requires a filter delegate type.
‫So it's really something interesting because you can on one hand just go ahead and create a method that
‫follows the delegate description and just use it as we saw here.
‫But you can also just create a variable which uses an anonymous method using the delegate keyword,
‫as we have done here, and install it inside of this filter and then use that filter here.
‫So this will now, of course, not be kids, but it will be between 20 and 30 and it will display all
‫the people that we have there.
‫So let's run this code and see if that works.
‫And we see here between 20 and 30, we have Anatoli, who is 25 years old, but no one else.
‫So now another approach of an anonymous method, and that is that we can pass an anonymous method directly
‫as a parameter.
‫So let's do that real quick.
‫So we call this display people method and this time I'm just going to display all people, right?
‫So here all people.
‫But now we need to pass in, you see here a filter delegate.
‫So let's go ahead and create an anonymous method.
‫And in order to do that, we need to use the delegate keyword, right?
‫They'll get like so and we need to of course still follow the same instruction.
‫We still need to make sure that our delegate that we're passing is going to be of type filter delegate
‫if we look at it here at the top.
‫So here this filter delegate needed to return a pool and it needed a person.
‫So let's get in the person as the parameter P But then of course, as I stated, we need to also return
‫something like a boolean.
‫So basically we can just call it like so.
‫So here this is a very condensed line of code.
‫There is a lot going on here.
‫We're displaying people, but we also have a filter here and this filter, it takes all the people and
‫it just returns true because it really doesn't matter that much which person we put in here.
‫So that's basically it.
‫Otherwise, of course you can also write it like so this will maybe be a little more readable.
‫So this here is our anonymous method that we are passing as the third parameter as we are just following
‫the filter delegate.
‫Set up.
‫So you see here we're returning a bull and we are taking in a person as the parameter.
‫So now you see the power of delegates because they are super flexible.
‫They really just want you to follow a certain structure.
‫The rest is up to you.
‫And this gives you as a developer a lot of flexibility when working with delegates.
‫All right.
‫So that was a little introduction into anonymous methods.
‫Let's next look at Lambda Expressions.