Click here to Skip to main content
13,736,547 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


151 bookmarked
Posted 14 Dec 2011
Licenced CPOL

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

GeneralReason for my vote of 5 I like it. this article provides goo... Pin
H.A.Baig15-Dec-11 18:18
memberH.A.Baig15-Dec-11 18:18 

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 | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180920.1 | Last Updated 5 Mar 2013
Article Copyright 2011 by Dan Avidar
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid