Click here to Skip to main content
11,704,472 members (57,578 online)
Click here to Skip to main content

Tagged as

Understand Lambda Expressions in 3 minutes

, 5 Mar 2013 CPOL 277.7K 112
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.

    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)

Share

About the Author

Dan Avidar
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionThanks Pin
afablulo4-Jun-14 14:06
memberafablulo4-Jun-14 14:06 
GeneralMy vote of 5 Pin
Paw Møller3-Jun-14 14:07
memberPaw Møller3-Jun-14 14:07 
QuestionNicely written Pin
StarsAreUs2-Jun-14 13:28
memberStarsAreUs2-Jun-14 13:28 
AnswerRe: Nicely written Pin
Divyam Sharma22-May-15 19:38
professionalDivyam Sharma22-May-15 19:38 
QuestionThanks Pin
Andrew Torrance1-Apr-14 23:48
memberAndrew Torrance1-Apr-14 23:48 
QuestionThanks! Pin
proloser24-Feb-14 14:30
memberproloser24-Feb-14 14:30 
QuestionNice but the last link is obsolete Pin
rj475625-Jan-14 18:13
memberrj475625-Jan-14 18:13 
AnswerRe: Nice but the last link is obsolete Pin
Nick Alexeev16-Apr-14 9:25
memberNick Alexeev16-Apr-14 9:25 
GeneralRe: Nice but the last link is obsolete Pin
rj475621-Apr-14 18:14
memberrj475621-Apr-14 18:14 
QuestionMy vote of 5 Pin
Member 323305517-Dec-13 19:53
memberMember 323305517-Dec-13 19:53 
GeneralThe simplest article of confused topic so far. Thank You Pin
Deepak K Gupta29-Sep-13 10:06
memberDeepak K Gupta29-Sep-13 10:06 
GeneralMy vote of 5 Pin
Tarun Y Mangukiya2-Sep-13 4:57
memberTarun Y Mangukiya2-Sep-13 4:57 
GeneralMy vote of 5 Pin
Edgar ramirez24-Aug-13 7:54
professionalEdgar ramirez24-Aug-13 7:54 
QuestionSorry... Pin
jhewitt999923-Aug-13 11:45
memberjhewitt999923-Aug-13 11:45 
AnswerRe: Sorry... Pin
Spong3bob8-May-15 12:57
memberSpong3bob8-May-15 12:57 
QuestionNice intro, but could expand Pin
ShaunnyBoy21-Aug-13 2:01
memberShaunnyBoy21-Aug-13 2:01 
QuestionNicely explained...Very thanks Pin
Chintan.Desai31-Jul-13 18:01
groupChintan.Desai31-Jul-13 18:01 
GeneralMy vote of 5 Pin
Brian A Stephens1-Jul-13 2:33
professionalBrian A Stephens1-Jul-13 2:33 
GeneralMy vote of 4 Pin
Adam Nain12-Jun-13 20:08
memberAdam Nain12-Jun-13 20:08 
GeneralMy vote of 5 Pin
Javier Prieto12-Jun-13 3:40
memberJavier Prieto12-Jun-13 3:40 
QuestionMy vote of 5 Pin
vikram00110-Jun-13 21:37
membervikram00110-Jun-13 21:37 
GeneralMy vote of 5 Pin
N_cooL5-Jun-13 21:55
memberN_cooL5-Jun-13 21:55 
QuestionUnderstand Lambda Expressions Pin
Endalew26-May-13 15:20
memberEndalew26-May-13 15:20 
GeneralMy vote of 5 Pin
VitorHugoGarcia12-May-13 22:26
memberVitorHugoGarcia12-May-13 22:26 
Questionty Pin
Member 1000908125-Apr-13 13:10
memberMember 1000908125-Apr-13 13:10 
GeneralMy vote of 5 Pin
du[DE]26-Mar-13 4:28
memberdu[DE]26-Mar-13 4:28 
GeneralMy vote of 5 Pin
KRISHNA ANIPINDI24-Mar-13 23:52
memberKRISHNA ANIPINDI24-Mar-13 23:52 
GeneralMy vote of 5 Pin
GuyThiebaut18-Mar-13 6:27
memberGuyThiebaut18-Mar-13 6:27 
GeneralWell Explained Pin
PratimaG11-Mar-13 5:11
memberPratimaG11-Mar-13 5:11 
GeneralMy vote of 4 Pin
iAmit196-Mar-13 17:36
memberiAmit196-Mar-13 17:36 
QuestionVery well explained Pin
Member 93414686-Mar-13 6:48
memberMember 93414686-Mar-13 6:48 
AnswerRe: Very well explained Pin
Dan Avidar6-Mar-13 7:20
memberDan Avidar6-Mar-13 7:20 
GeneralMy vote of 5 Pin
Omid Babaei6-Mar-13 0:33
memberOmid Babaei6-Mar-13 0:33 
GeneralMy vote of 5 Pin
sconnor28-Jan-13 16:36
membersconnor28-Jan-13 16:36 
GeneralMy vote of 5 Pin
patil9927-Jan-13 23:15
memberpatil9927-Jan-13 23:15 
GeneralMy vote of 1 Pin
Nemo19665-Dec-12 12:16
memberNemo19665-Dec-12 12:16 
GeneralRe: My vote of 1 Pin
cigwork5-Dec-12 21:15
membercigwork5-Dec-12 21:15 
GeneralRe: My vote of 1 Pin
Roger Tranchez15-Dec-12 23:35
memberRoger Tranchez15-Dec-12 23:35 
QuestionWhat about the link to linqtutorial.net? Pin
Klaus Luedenscheidt4-Dec-12 19:34
memberKlaus Luedenscheidt4-Dec-12 19:34 
QuestionCongratulations Pin
Glen Harvy4-Dec-12 17:51
memberGlen Harvy4-Dec-12 17:51 
GeneralMy vote of 5 Pin
Nosugar3-Oct-12 23:39
memberNosugar3-Oct-12 23:39 
QuestionMy Vote of 5! Pin
akosidab18-Jul-12 23:01
memberakosidab18-Jul-12 23:01 
QuestionUnderstand Lambda Expression in 3 min - Wonderfull Pin
Sen K Mathew16-Jul-12 1:11
memberSen K Mathew16-Jul-12 1:11 
GeneralReason for my vote of 5 Great and To the point Pin
TanzeelurRehman13-Jan-12 0:26
memberTanzeelurRehman13-Jan-12 0:26 
GeneralReason for my vote of 5 Short and to the point. Very clear a... Pin
Pantelis Chatzinikolis1-Jan-12 15:16
memberPantelis Chatzinikolis1-Jan-12 15:16 
GeneralReason for my vote of 5 Nice article. Clarifies on the very ... Pin
Anandachetan Elikapati29-Dec-11 15:44
memberAnandachetan Elikapati29-Dec-11 15:44 
GeneralWhat is a declartion? Pin
jenkstom20-Dec-11 3:38
memberjenkstom20-Dec-11 3:38 
GeneralReason for my vote of 5 Great, easy to understand. Pin
chuckamus_prime20-Dec-11 3:04
memberchuckamus_prime20-Dec-11 3:04 
GeneralReason for my vote of 3 its easy to understand, but more exa... Pin
Mohammad_Denmark19-Dec-11 22:45
memberMohammad_Denmark19-Dec-11 22:45 
GeneralReason for my vote of 5 It's easy to understand. :) Pin
Baoyu Mou19-Dec-11 16:57
memberBaoyu Mou19-Dec-11 16:57 

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.150819.1 | Last Updated 5 Mar 2013
Article Copyright 2011 by Dan Avidar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid