Click here to Skip to main content
13,350,483 members (68,500 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


146 bookmarked
Posted 14 Dec 2011

Understand Lambda Expressions in 3 Minutes

, 5 Mar 2013
Rate this:
Please Sign up or sign in to vote.
Lambda expressions short tutorial


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.

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.


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

Lambda basic definition: Parameters => Executed code

  1. What is a Lambda Expression?
  2. Why do we need lambda expressions? (Why would we need to write a method without a name?)
    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.
  3. How do we define a lambda expression?

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.


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

You may also be interested in...


Comments and Discussions

AnswerRe: Very well explained Pin
Dan Avidar6-Mar-13 8:20
memberDan Avidar6-Mar-13 8:20 
GeneralMy vote of 5 Pin
Omid Babaei6-Mar-13 1:33
memberOmid Babaei6-Mar-13 1:33 
GeneralMy vote of 5 Pin
sconnor28-Jan-13 17:36
membersconnor28-Jan-13 17:36 
GeneralMy vote of 5 Pin
patil9928-Jan-13 0:15
memberpatil9928-Jan-13 0:15 
GeneralMy vote of 1 Pin
Nemo19665-Dec-12 13:16
memberNemo19665-Dec-12 13:16 
GeneralRe: My vote of 1 Pin
cigwork5-Dec-12 22:15
membercigwork5-Dec-12 22:15 
GeneralRe: My vote of 1 Pin
Roger Tranchez16-Dec-12 0:35
memberRoger Tranchez16-Dec-12 0:35 
QuestionWhat about the link to Pin
Klaus Luedenscheidt4-Dec-12 20:34
memberKlaus Luedenscheidt4-Dec-12 20:34 
First congratulations to your good article. Never seen such a dense and easy explantation of linq before.

I tried to follow the link to the tutorial but there is an Error 502 (bad gateway). A WHoIs query told me that the domain has expired. Is this really so or does the firewall in my cokmpany fooling me?

QuestionCongratulations Pin
Glen Harvy4-Dec-12 18:51
memberGlen Harvy4-Dec-12 18:51 
GeneralMy vote of 5 Pin
Nosugar4-Oct-12 0:39
memberNosugar4-Oct-12 0:39 
QuestionMy Vote of 5! Pin
akosidab19-Jul-12 0:01
memberakosidab19-Jul-12 0:01 
QuestionUnderstand Lambda Expression in 3 min - Wonderfull Pin
Sen K Mathew16-Jul-12 2:11
memberSen K Mathew16-Jul-12 2:11 
GeneralReason for my vote of 5 Great and To the point Pin
TanzeelurRehman13-Jan-12 1:26
memberTanzeelurRehman13-Jan-12 1:26 
GeneralReason for my vote of 5 Short and to the point. Very clear a... Pin
Pantelis Chatzinikolis1-Jan-12 16:16
memberPantelis Chatzinikolis1-Jan-12 16:16 
GeneralReason for my vote of 5 Nice article. Clarifies on the very ... Pin
Anandachetan Elikapati29-Dec-11 16:44
memberAnandachetan Elikapati29-Dec-11 16:44 
GeneralWhat is a declartion? Pin
jenkstom20-Dec-11 4:38
memberjenkstom20-Dec-11 4:38 
GeneralReason for my vote of 5 Great, easy to understand. Pin
chuckamus_prime20-Dec-11 4:04
memberchuckamus_prime20-Dec-11 4:04 
GeneralReason for my vote of 3 its easy to understand, but more exa... Pin
Mohammad_Denmark19-Dec-11 23:45
memberMohammad_Denmark19-Dec-11 23:45 
GeneralReason for my vote of 5 It's easy to understand. :) Pin
Baoyu Mou19-Dec-11 17:57
memberBaoyu Mou19-Dec-11 17:57 
General:) Think I have spent too long trying to fight the PC establ... Pin
Bob100019-Dec-11 15:12
memberBob100019-Dec-11 15:12 
GeneralReason for my vote of 1 ou used the most irritating of phase... Pin
Bob100019-Dec-11 12:55
memberBob100019-Dec-11 12:55 
GeneralRe: Ok, I will give it my attention next time :) Pin
Ronaldo jer19-Dec-11 14:06
memberRonaldo jer19-Dec-11 14:06 
GeneralRe: Reason for my vote of 1ou used the most irritating of phase... Pin
Spiff Dog4-Dec-12 14:52
memberSpiff Dog4-Dec-12 14:52 
GeneralRe: Reason for my vote of 1ou used the most irritating of phase... Pin
Bob10004-Dec-12 23:22
memberBob10004-Dec-12 23:22 
GeneralRe: Reason for my vote of 1ou used the most irritating of phase... Pin
Spiff Dog13-Dec-12 13:10
memberSpiff Dog13-Dec-12 13:10 

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

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

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