Part 99 Lambda expression in c#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
part 99 of c-sharp tutorial in this video we'll discuss what lambda expressions are with an example this is continuation to part 98 so please watch what 98 before proceeding in this video we'll be working with the same example that we worked with in the previous session so what are lambda expressions anonymous methods and lambda expressions are very similar anonymous methods were introduced in c-sharp 2 and lambda expressions in c-sharp 3 in most cases lambda expressions supersede anonymous methods if you recollect from the previous session we have used anonymous method as you can see here to find an employee with ID equals 1 0 2 from the list of employees that we have caught and notice the usage of anonymous method here we explicitly use the delegate keyword and then we specify the type of the input parameter here the type of the input parameter is employee the same example is rewritten here using a lambda expression and notice how simple is this lambda expression first of all this operator right here is called as lambda operator and it reads as goes to here EMP goes to EMP dot i DW equals 1 0 2 so that's the lambda operator and what is this EMP on the left hand side this is nothing but the input parameter okay now first of all notice that we are not explicitly using the delegate keyword within the lambda expression and number 2 we're not specifying the type of the input parameter okay so how is the lambda expression going to know the type of this it is going to infer that automatically look at this fine method it's being invoked on this list employee so object what is this list employees object it is of type list employee so e and B here is going to be automatically inferred to employee time let's actually look at this in action let's flip to visual studio so this is an anonymous method now let's replace this with a lambda expression so find EMP goes to and look at this when I hover my mouse over look at that it says EMP is of type employee so the type is being automatically input and we are not using the delegate keyword explicitly here and we can say EMP ID double equals 1 0 2 let's go ahead and run this and see if we get the same output actually look at that we get the same output here they're using a lambda expression notice how simple it is to use they're more convenient than anonymous method now here the name EMP you know it's just a name any meaningful name you can give it X for example if you want to okay so this is the input parameter name and this is the expression and this equals greater than it's nothing but the lambda operator all right so now here we haven't specified explicitly the type of the input parameter now for some reasons if you want to be explicit and if you prefer specifying the type of the input parameter you can do so as well look at this here you know we are specifying the type of the input parameter explicitly but that's not required which means we can rewrite this one you know like this so include a bracket and that X is of type employee okay and let's run this and lips see if it works the same way look at this it works the same way so you can or you know you may omit the type of the input parameter that's going to be automatically inferred all right so this operator is called as lambda operator and we read that as goes to notice that with the lambda expression we don't have to use the delegate keyword explicitly and we also don't have to specify the input parameter type explicitly the parameter type is automatically inferred lambda expressions are more convenient to use than anonymous methods we have seen that in action just now lambda expressions are particularly helpful for writing link query expressions let's flip to this in our demo that we have let's bring in the link namespace so using system dot link let's say for example from this list I want to find out all the employees whose name starts with letter M now in that case we can use the count function so let's go and use the count function so int count so that function is going to return the number of employees which is a type integer so list employees dot count and if you look at this one again you know there are two overloads the other overload is expecting a predicate to be passed now we'll discuss what this funk is all about in a later video session but basically you know we need to specify our predicate here ok so again we can use lambda expressions so what do we want to do we want to find out all the employees the count of all the employees whose name starts with letter M so how are we going to do that so again you can give the input parameter any name you want so I can give X so X such that now notice that here we have used you know the name as X the input parameter again we are using X here now what is the scope of this parameter X within you know right there you cannot use outside of that scope okay so that's why we can reuse it here if we want to okay so X goes to X dot name of the employee dot starts with and you want to find all the employees whose name starts with letter M and look at this this count method is actually returning an integer we're showing that in this variable and let's go ahead and print that out so console dot readline let's just say count equals whatever is present in our variable all right so we should get two as the output so count equals two right so in most of the cases lambda expressions supersedes anonymous methods to my knowledge the only time I prefer to use an anima smething or lambda expressions is when we have to omit the parameter list when it's not used within the body so anonymous methods I love the parameter list to be omitted entirely when it's not used within the body whereas that's not the case with lambda expressions let's understand the statement with an example now if you recollect from the previous session we worked with this windows forms application as well and what did we do within the form load event we have created at this button control dynamically if I say in the text property and to the click event we have you know hooked up and even handler using an anonymous method here now if you look at this click event so let's navigate to the event so here we have the click event and if you look at this event handler and go to the definition on that that's again a delegate and it expects you know two parameters one is of type object the other is of type eventargs and it's not going to return anything okay so now the delegate that we are going to create here we need to pass you know basically two parameters that is object and eventargs now with the anonymous methods you know we can write this piece of code right here let's actually copy this it and paste them right there and let's change the names let's call this event sender and let's call this eventargs okay now let's go ahead and run this it's going to work okay let's click that button clicked okay so now this is matching with what we have right here okay so it compiles it works fine so here we are using anonymous method now I can get rid of these parameters altogether because within the body right here we are not using those parameters okay so when I get rid of those piranhas meters look at this first of all when we build this in the build status bar bill succeeded when we run this it's going to work as well without any problem ok button click now this anonymous method can be replaced with a lambda expression okay so that's the anonymous method and this is the lambda expression look at that we are using that lambda operator and here we are specifying the parameters okay now within the body again we are not using parameters these two parameters are not being used within the body but what's going to happen if we omit those parameters we will get a compilation error let's actually check that in action so now we have got two parameters here to be passed now how will we know what are those parameter types look at this the moment we open that you know this click is an event and you know the delegate that is associated with that is this one even handler it's expecting these are the two parameters so let's actually copy them and let's go to the form so again here do we need to specify the names of the I mean the types of the parameters you know not required the lambda expression can automatically you know infer them okay but if you want to be explicit you can specify them like so so those are the two input parameters that's the lambda operator and here we have our expression okay so let's go ahead and run this first of all so it works let's click that so the application is working is expected now we don't have to explicitly specify the type so let's get rid of them and see if it still works so let's run this click the button so it still works now let's actually get rid of these parameters altogether before we do that look at this when I hover the mouse over look at this this is of type object even sender and eventargs is of type even tardes okay so the input parameter types are automatically inferred now what do you think is going to happen if I get rid of these to save it and let's build the application look at this delegate system have event handler does not take zero arguments so irrespective of whether we are using those parameters are not within the board a we still have to specify them and we use lambda but whereas that's not the case with anonymous methods and only most methods allow the parameter list to be omitted entirely when it's not used within the body all right that's it for today thank you for listening have a great day
Info
Channel: kudvenkat
Views: 295,779
Rating: undefined out of 5
Keywords: lamdba, expression, c#, example, c# tutorial, simple lambda expression example c#, lambda expression in c# tutorial, lambda expression tutorial, lambda vs anonymous function, lambda vs anonymous methods, lambda vs anonymous function c#, c# anonymous function vs lambda expressions
Id: LDgQ-spnrYY
Channel Id: undefined
Length: 11min 27sec (687 seconds)
Published: Sun Mar 23 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.