Click here to Skip to main content
13,254,077 members (64,682 online)
Click here to Skip to main content
Add your own
alternative version


35 bookmarked
Posted 5 Feb 2010

Lambda Expression in C# 3.0

, 8 Feb 2010
Rate this:
Please Sign up or sign in to vote.
Lambda Expression is one of the best features of C# 3.0

Lambda Expression is one of the best features of C# 3.0. Lambda expression is the same as anonymous method introduced in C# 2.0. Lambda Expression is a concise syntax to achieve the same goal as anonymous method. Now we can summarize Lambda expression in one line.

“Lambda expression is simply a Method.”

Syntax of Lambda Expression

Input Parameters => Expression/Statement Block;

Left hand side of expression contains zero or more parameters followed by Lambda operator ‘=>’ which is read as “goes to” and right hand side contains the expression or Statement block.

A simple Lambda expression:

x => x * 2 

This Lambda expression is read as “x goes to x times 2”. Lambda Operator “=>” has the same precedence as assignment “=” operator. This simple expression takes one parameter “x” and returns the value “x*2”.

Parameters Type 

The parameters of the lambda expression can be explicitly or implicitly typed. For example:

(int p) => p * 4; 	// Explicitly Typed Parameter
q => q * 4; 	// Implicitly Typed Parameter

Explicit typed parameter is the same as parameters of method where you explicitly specified the type of parameter. In an implicit typed parameter, the type of parameter is inferred from the context of lambda expression in which it occurs.

Type Inference is a new feature of C# 3.0. I will explain it in some other blog.

Use Simple Lambda Expression

Here is a simple example of Lambda Expression which returns a list of numbers greater than 8.

int[] numbers = {1,2,3,4,5,6,7,8,9,10 };   
var  returnedList = numbers.Where(n => (n > 8));

You can also use anonymous method for returning the same list.

int[] numbers = {1,2,3,4,5,6,7,8,9,10 };
var returnedList = numbers.Where(delegate(int i) { return i > 8; });

Use Statement Block in Lambda Expression

Here is a simple example to write a statement block in the lambda expression. This expression returns a list of numbers less than 4 and greater than 8.

int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

var returnedList = numbers.Where(n =>
                              if (n < 4)
                                  return true;
                              else if (n > 8)
                                  return true;

                               return false;

Use Lambda with More than One Parameter

You can also write lambda which takes more than one parameter. Here is an example of lambda which adds two integer numbers:

delegate int AddInteger(int n1, int n2);

AddInteger addInt = (x, y) => x + y;
int result = addInt(10,4); // return 14

Use Lambda with Zero Parameter

Here is an example of lambda which takes no parameter and returns new Guid.

delegate Guid GetNextGuid();

GetNextGuid getNewGuid = () => (Guid.NewGuid());
Guid newguid = getNewGuid();

Use Lambda that Returns Nothing

You can also write lambda which returns void. Here is an example that lambda is only showing message and returns nothing.

delegate void ShowMessageDelegate();

ShowMessageDelegate msgdelegate = () => MessageBox.Show("It returns nothing.");

Some Build in Delegates

.NET Framework 3.0 provides some build in parameterized delegate types that is “Func<T>(...)” and also provides some parameterized delegates that return void that is “Action<T>(...)”.


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


About the Author

Shakeel Iqbal
Software Developer (Senior) TEO
Pakistan Pakistan
Passion and positive dedication is essential part of success. I believe on hardworking and sharing knowledge with others. I always try to be a better than I am and think positive for positive result.

My Blogs

You may also be interested in...

Comments and Discussions

QuestionMy vote of 4 Pin
Janilane11-Oct-13 19:56
memberJanilane11-Oct-13 19:56 
GeneralMy vote of 3 Pin
Amir Mehrabi-Jorshary20-Oct-10 1:32
memberAmir Mehrabi-Jorshary20-Oct-10 1:32 
GeneralMy vote of 2 Pin
yannlh9-Feb-10 7:38
memberyannlh9-Feb-10 7:38 
GeneralMy vote of 1 Pin
PanagiotisG9-Feb-10 6:23
memberPanagiotisG9-Feb-10 6:23 
Nothing that doesn't already exist in the beginner articles that can be found on the internet.
GeneralJust a question Pin
the_void8-Feb-10 13:26
memberthe_void8-Feb-10 13:26 
GeneralRe: Just a question Pin
Shakeel Iqbal8-Feb-10 19:57
memberShakeel Iqbal8-Feb-10 19:57 

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
Web01 | 2.8.171114.1 | Last Updated 9 Feb 2010
Article Copyright 2010 by Shakeel Iqbal
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid