Click here to Skip to main content
11,413,676 members (73,112 online)
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?
  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.


    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


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

Comments and Discussions

Questionlink to "more info" is a virus Pin
Member 10539475, 1-Feb-15 14:36
memberMember 105394751-Feb-15 14:36 
GeneralMy vote of 4 Pin
akshay0, 8-Jan-15 2:14
memberakshay08-Jan-15 2:14 
GeneralMy vote of 5 Pin
YannisPs, 5-Nov-14 1:59
memberYannisPs5-Nov-14 1:59 
QuestionThank you soooo much Pin
Don Driskell, 21-Oct-14 8:53
memberDon Driskell21-Oct-14 8:53 
GeneralPerfect Article Pin
Bala_Subramani, 26-Jun-14 2:48
memberBala_Subramani26-Jun-14 2:48 
GeneralMy vote of 5 Pin
sandeepmgupta, 10-Jun-14 23:13
membersandeepmgupta10-Jun-14 23:13 
GeneralMy vote of 5 Pin
Dhanu786, 5-Jun-14 20:36
memberDhanu7865-Jun-14 20:36 
QuestionThanks Pin
afablulo, 4-Jun-14 15:06
memberafablulo4-Jun-14 15:06 
GeneralMy vote of 5 Pin
Paw Møller, 3-Jun-14 15:07
memberPaw Møller3-Jun-14 15:07 
QuestionNicely written Pin
StarsAreUs, 2-Jun-14 14:28
memberStarsAreUs2-Jun-14 14:28 
QuestionThanks Pin
Andrew Torrance, 2-Apr-14 0:48
memberAndrew Torrance2-Apr-14 0:48 
QuestionThanks! Pin
proloser2, 4-Feb-14 15:30
memberproloser24-Feb-14 15:30 
QuestionNice but the last link is obsolete Pin
rj4756, 25-Jan-14 19:13
memberrj475625-Jan-14 19:13 
AnswerRe: Nice but the last link is obsolete Pin
Nick Alexeev, 16-Apr-14 10:25
memberNick Alexeev16-Apr-14 10:25 
GeneralRe: Nice but the last link is obsolete Pin
rj4756, 21-Apr-14 19:14
memberrj475621-Apr-14 19:14 
QuestionMy vote of 5 Pin
Member 3233055, 17-Dec-13 20:53
memberMember 323305517-Dec-13 20:53 
GeneralThe simplest article of confused topic so far. Thank You Pin
Deepak K Gupta, 29-Sep-13 11:06
memberDeepak K Gupta29-Sep-13 11:06 
GeneralMy vote of 5 Pin
Tarun Y Mangukiya, 2-Sep-13 5:57
memberTarun Y Mangukiya2-Sep-13 5:57 
GeneralMy vote of 5 Pin
Edgar ramirez, 24-Aug-13 8:54
professionalEdgar ramirez24-Aug-13 8:54 
QuestionSorry... Pin
jhewitt9999, 23-Aug-13 12:45
memberjhewitt999923-Aug-13 12:45 
QuestionNice intro, but could expand Pin
ShaunnyBoy, 21-Aug-13 3:01
memberShaunnyBoy21-Aug-13 3:01 
QuestionNicely explained...Very thanks Pin
Chintan.Desai, 31-Jul-13 19:01
groupChintan.Desai31-Jul-13 19:01 
GeneralMy vote of 5 Pin
Brian A Stephens, 1-Jul-13 3:33
professionalBrian A Stephens1-Jul-13 3:33 
GeneralMy vote of 4 Pin
Adam Nain, 12-Jun-13 21:08
memberAdam Nain12-Jun-13 21:08 
GeneralMy vote of 5 Pin
Javier Prieto, 12-Jun-13 4:40
memberJavier Prieto12-Jun-13 4:40 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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