C# LAMBDA Expressions and ANONYMOUS Functions Tutorial | 2021

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up youtube this is dennis ponita for tutorials.eu in this video you are going to learn about lambda expressions as well as anonymous functions in c sharp so this will help you to understand these complex topics that are really powerful when it comes to advancing your c sharp skills but before we get started i would really appreciate if you hit that like button it really helps us out and also subscribe if you haven't done so already and now let's get started an anonymous method is a method without a name well who guessed 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 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 called 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 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 at the 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 is really important here otherwise this won't work because this is basically like creating a variable okay so it's like int x equals three it's pretty much the same thing that we're doing here with this filter delegate 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 filter you might recall display people always had the 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 anatoly 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 okay 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 bool 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's 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 setup 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 quick pause in this video you'll learn something about c sharp and if you want to learn everything there is to know that you need for the fundamentals and to become a real c sharp developer then definitely check out my c sharp master class in which you are going to learn all of the things you need to know about c sharp so you're going to learn how to do the basics how to use object oriented programming how to use wpf in order to create your own user interfaces how to use databases how to use link how to create your own games using unity and a lot more so if you want to become a realty shop developer definitely check out the link in the description below alright so we saw in the previous lecture how anonymous methods can help us to write the code blocks inline 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's 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 or search for a letter and then i'm going to call the display people method once again and i'm just going to say i'm want to display everything where the h is above 20 with a search keyword so that means we're looking for everyone 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 the search keyword to this statement here okay 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 okay 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 okay so here we're just saying these are all the parameters equal greater than and this is just one parameter which is why it's fine to not use brackets otherwise i would have so two surrounded 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 age 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 delegate 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 fill set it up all right and now we of course need also to add an else block here so elsewhere 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 cool 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 ph 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 achieved 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 anatoly is 25 years old and aiden is also greater than 20 with a search keyword a so these are the two up here and then who's exactly 25 well that's only anatoly okay that's pretty much it thanks a lot for watching this video now you know how anonymous functions slash methods as well as lambda expressions work i hope you enjoyed this video and leave a like if you did so it would really help us out also hit that subscribe button because we're uploading a lot more c-sharp content regularly here as well as well unity content which is also c-sharp content so to speak so see you in the next video
Info
Channel: tutorialsEU
Views: 2,347
Rating: undefined out of 5
Keywords: Tutorials, Tutorial, Programming, Course, Learn, Step by step, guide, development, programmer, video course, video tutorial, learn how to, how to, lambda expression in c# examples, lambda expression in c# tutorial for beginners, lambda expression in linq c# examples, lambda expressions in c# entity framework linq, why we use lambda expression in c#, c# anonymous function, anonymous methods and lambda expressions in c#, anonymous method in c# geeksforgeeks
Id: 4gNeU539a4M
Channel Id: undefined
Length: 14min 30sec (870 seconds)
Published: Mon Aug 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.