Click here to Skip to main content
Click here to Skip to main content

Tagged as

Understand Lambda Expressions in 3 minutes

, 5 Mar 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Lambda expressions short tutorial.
  1. What is a Lambda Expression?

  3. Why do we need lambda expressions? (Why would we need to write a method without a name?)
  4. Convenience. It's a shorthand that allows you to write a method in the same place you are going to use it. Especially useful in places where a method is being used only once, and the method definition is short. It saves you the effort of declaring and writing a separate method to the containing class.


    1. Reduced typing. No need to specify the name of the function, its return type, and its access modifier. 
    2. When reading the code you don't need to look elsewhere for the method's definition. 

    Lambda expressions should be short. A complex definition makes the calling code difficult to read. 

  5. How do we define a lambda expression? 
  6. Lambda basic definition: Parameters => Executed code.

Simple example

n => n % 2 == 1 
  • n is the input parameter
  • n % 2 == 1 is the expression

You can read n => n % 2 == 1 like: "input parameter named n goes to anonymous function which returns true if the input is odd".

Same example (now execute the lambda):

//Now oddNumbers is equal to 11 and 37

That's all, now you know the basics of Lambda Expressions.

  • * I didn't mention expression trees/run time advantages of lambda expression due to limited scope.
  • * Reference Lambda Expression tutorial




About the Author

Dan Avidar

United States United States
No Biography provided

Comments and Discussions

QuestionMy Vote of 5! Pinmemberakosidab19-Jul-12 0:01 



| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 5 Mar 2013
Article Copyright 2011 by Dan Avidar
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid