Click here to Skip to main content
15,861,172 members
Articles / Programming Languages / C#
Tip/Trick

Understand Lambda Expressions in 3 Minutes

Rate me:
Please Sign up or sign in to vote.
4.88/5 (231 votes)
5 Mar 2013CPOL1 min read 1.1M   151   76
Lambda expressions short tutorial

Introduction

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.

Benefits

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

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

C#
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.

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionLambda expressions are type safe Pin
battlmonstr21-Aug-16 3:42
battlmonstr21-Aug-16 3:42 
QuestionCouldn't we just use the logical expression instead of using it along with lambda expression ? Pin
Chandrabhan Pradhan5-Aug-16 7:43
Chandrabhan Pradhan5-Aug-16 7:43 
QuestionKISS Pin
rahulchow22-Jul-16 8:42
rahulchow22-Jul-16 8:42 
PraiseReally helpful Pin
Syed Baqer Naqvi26-Jan-16 19:10
professionalSyed Baqer Naqvi26-Jan-16 19:10 
Generalits reference link gets spam message Pin
Southmountain19-Dec-15 10:10
Southmountain19-Dec-15 10:10 
QuestionEasiest way to explain it Pin
Vikas Thombre3-Oct-15 6:50
Vikas Thombre3-Oct-15 6:50 
QuestionSuper simple explanation Pin
Member 109346763-Aug-15 22:02
Member 109346763-Aug-15 22:02 
GeneralMy vote of 5 Pin
Member 110351022-Aug-15 9:55
Member 110351022-Aug-15 9:55 
GeneralMy vote of 5 Pin
Bunny Charles27-Apr-15 21:10
professionalBunny Charles27-Apr-15 21:10 
Questionlink to "more info" is a virus Pin
Member 105394751-Feb-15 13:36
Member 105394751-Feb-15 13:36 
GeneralMy vote of 4 Pin
akshay08-Jan-15 1:14
akshay08-Jan-15 1:14 
GeneralMy vote of 5 Pin
YannisPs5-Nov-14 0:59
YannisPs5-Nov-14 0:59 
QuestionThank you soooo much Pin
Don Driskell21-Oct-14 7:53
Don Driskell21-Oct-14 7:53 
I recently started a new job where Lambda expressions are used extensively. Your "3 Minute" article was right on the money and explained the basics very clearly. Thank you!!!
GeneralPerfect Article Pin
BalasubramaniM26-Jun-14 1:48
BalasubramaniM26-Jun-14 1:48 
GeneralMy vote of 5 Pin
sandeepmgupta10-Jun-14 22:13
sandeepmgupta10-Jun-14 22:13 
GeneralMy vote of 5 Pin
dhanashri_andhare5-Jun-14 19:36
dhanashri_andhare5-Jun-14 19:36 
QuestionThanks Pin
afablulo4-Jun-14 14:06
afablulo4-Jun-14 14:06 
GeneralMy vote of 5 Pin
Paw Møller3-Jun-14 14:07
Paw Møller3-Jun-14 14:07 
QuestionNicely written Pin
StarsAreUs2-Jun-14 13:28
StarsAreUs2-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
Andrew Torrance1-Apr-14 23:48 
QuestionThanks! Pin
proloser24-Feb-14 14:30
proloser24-Feb-14 14:30 
QuestionNice but the last link is obsolete Pin
rj475625-Jan-14 18:13
rj475625-Jan-14 18:13 
AnswerRe: Nice but the last link is obsolete Pin
Nick Alexeev16-Apr-14 9:25
professionalNick Alexeev16-Apr-14 9:25 
GeneralRe: Nice but the last link is obsolete Pin
rj475621-Apr-14 18:14
rj475621-Apr-14 18:14 

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.