Lambda expressions seem to be a common stumbling block to those new to .NET. Does anyone have a good resource for explaining them to newbies?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A lambda is simply an anonymous method like this:
The first part of a lambda is for the method parameters. If there are no parameters, or if there are more than one parameter, parentheses are required:
A lambda corresponding to the anonymous method above would be written:
Depending on how the lambda is used, the compiler can actually infer the types of the parameters and the return value, so they aren’t always required.
The last part of a lambda is for the method body.
is the same as
If the body of the method is a single line, the lambda does not require a return keyword, nor curly braces:
Those are the basics. I hope this helps.