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

Understand Lambda Expressions in 3 minutes

By , 5 Mar 2013
 
  1. What is a Lambda Expression?
  2. A lambda expression is an anonymous function and it is mostly used to create delegates in LINQ. Simply put, it's a method without a declaration, i.e., access modifier, return value declaration, and name. 

  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.

    Benefits: 

    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):

List<int> numbers = new List<int>{11,37,52};
List<int> oddNumbers = numbers.where(n => n % 2 == 1).ToList();
//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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Dan Avidar

United States United States
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralRe: My vote of 1memberRoger Tranchez15-Dec-12 23:35 
QuestionWhat about the link to linqtutorial.net?memberKlaus Luedenscheidt4-Dec-12 19:34 
QuestionCongratulationsmemberGlen Harvy4-Dec-12 17:51 
GeneralMy vote of 5memberNosugar3-Oct-12 23:39 
QuestionMy Vote of 5!memberakosidab18-Jul-12 23:01 
QuestionUnderstand Lambda Expression in 3 min - WonderfullmemberSen K Mathew16-Jul-12 1:11 
GeneralReason for my vote of 5 Great and To the pointmemberTanzeelurRehman13-Jan-12 0:26 
Reason for my vote of 5
Great and To the point
GeneralReason for my vote of 5 Short and to the point. Very clear a...memberPantelis Chatzinikolis1-Jan-12 15:16 
GeneralReason for my vote of 5 Nice article. Clarifies on the very ...memberAnandachetan Elikapati29-Dec-11 15:44 
GeneralWhat is a declartion?memberjenkstom20-Dec-11 3:38 
GeneralReason for my vote of 5 Great, easy to understand.memberchuckamus_prime20-Dec-11 3:04 
GeneralReason for my vote of 3 its easy to understand, but more exa...memberMohammad_Denmark19-Dec-11 22:45 
GeneralReason for my vote of 5 It's easy to understand. :)memberBaoyu Mou19-Dec-11 16:57 
General:) Think I have spent too long trying to fight the PC establ...memberBob100019-Dec-11 14:12 
GeneralReason for my vote of 1 ou used the most irritating of phase...memberBob100019-Dec-11 11:55 
GeneralRe: Ok, I will give it my attention next time :)memberRonaldo jer19-Dec-11 13:06 
GeneralRe: Reason for my vote of 1ou used the most irritating of phase...memberSpiff Dog4-Dec-12 13:52 
GeneralRe: Reason for my vote of 1ou used the most irritating of phase...memberBob10004-Dec-12 22:22 
GeneralRe: Reason for my vote of 1ou used the most irritating of phase...memberSpiff Dog13-Dec-12 12:10 
GeneralRe: Reason for my vote of 1ou used the most irritating of phase...memberBob100013-Dec-12 14:46 
GeneralRe: Reason for my vote of 1ou used the most irritating of phase...memberBob100013-Dec-12 14:47 
GeneralReason for my vote of 5 good onememberS.P. Tiwari15-Dec-11 21:48 
GeneralReason for my vote of 5 I like it. this article provides goo...memberH.A.Baig15-Dec-11 18:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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