C# - Lambda Expression

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
now in this particular video we are going to cover another very important topic that is lambda expression before continuing with lambda expression let me explain something about an expression suppose I have a scenario like I want to add a couple of numbers like int a is equal to C plus D we are C and B are some separate variables just having some values inside alright and let's compare this particular statement or expression with the method which is returning an integer called sum and inside this again we are doing the very same scenario like return C plus D all right so let me compare the functionality or the performance I should say between these two in this first case what is there C and D would be having their value they will be summed up and the result will go to a while here if I will have to call this sum so what will happen first of all this sum note sum method will be loaded in the call stack then maybe if there are parameters the parameters will be copied the sum the summing will be done and then the result will be returned and after all these things the call stack will be removing means this matter will be popped out from the call stack so this loading and unloading will be consuming some time as well and we can also notice like this particular thing is not that big you can even do this in a single line rather than calling a method so the output is the or the conclusion is that when the functionality is very small you can easily replace a method with an expression for a better functionality so here we are going to do the similar thing with lambda expressions as well as in the previous video we have noticed like we bound an anonymous method directly with a but if I'm not going to do a bigger task I can anytime replace that method Anonymous or any particular method with the expression called lambda expression so let's find it practically how can we do that for going through the implementation of lambda expressions I continue with the previous example as well here I have already defined the delegate having this structure and if I want to define a lambda expression for the same I will first of all replace the definition with the lambda expression here on the left hand side of lambda expression there will be a parameter then a goes to operator and then on the right hand side there will be a definition so here you can see I have defined my lambda expression and it is returning an integer since it is an expression you don't have to pass the return keyword as if I'll take an example here this is an expression where 2 and 4 will get multiplied and the value will be assigned to a but I don't have to write the return keyword right here all right similarly I have not written it here and whenever I will invoke this delegate with a particular value I will get something in return let's store that in the variable called result and then I will print it later to get the output in case you need to pass multiple arguments of different or same type like this you can also pass multiple parameters right here like this but make sure you are putting them in the parentheses if required you can also pass the data types along with these members and here what I'll do I just make a modification to this and obviously I'll have to pass the multiple parameters like this 4 and 6 both will get multiplied and 24 is the out let's say I don't want to return anything and I applied void in my delegate signature so of course as I said you don't have to write written keyword but ultimately it will return the value but in such situations what I'll have to do I'll have to keep the definition in the curly braces and inside this what you can do you can simply print the value like this since your delegate is not returning anything I will have to remove this and this so now I'm done with the implementation as you can see it is not giving me any error and I am not returning any value as well and in this same structure as well if you want to pass multiple statements in your lambda expression you can do that also like this all right so now when I invoke my delegate I will get the result program ended it is about the lambda expression
Info
Channel: Tutorials Point (India) Ltd.
Views: 54,833
Rating: undefined out of 5
Keywords: C# - Lambda Expression, Learn CSharp, Basic tutorial on CSharp, CSharp for beginners, fundamentals of CSharp, CSharp by Anadi Sharma, CSharp by Tutorialspoint
Id: kYeKFMf2mO8
Channel Id: undefined
Length: 4min 53sec (293 seconds)
Published: Mon Feb 05 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.