Click here to Skip to main content
12,547,501 members (47,773 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


31 bookmarked

Using Lambda Expression in C# 3

, 2 Apr 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
This article describes how to use Lambda expression in C# 3 to make developers' life easier.


Lambda expressions provide a more concise, functional syntax for writing anonymous methods.

Why Lambda Expressions

For better understanding about Lambda expression, let’s look at anonymous methods which are not new to C# 3.0 but are available from C# 2.0 itself. Anonymous methods mean code blocks can be written in-line where delegate values are expected. Somehow this anonymous method syntax is little bit difficult on functional programming and this is the point where lambda expressions come into action in C# 3.0.

Using Lambda Expressions

A lambda expression is written as a parameter list, followed by the => token, followed by an expression or a statement block.

( parameters ) => expr


  • i => i + 1; 
  • i => { return i + 1; } 

We can read this lambda expression as “goes to” so the above example can be read as “x goes to x+1”.

Note that here, we do not provide parameter types. If you need, you can define types also but nothing else adds more readability to your coding than a little more typing.

We can use multiple parameters also:

  • (i,j) => i + j;

Or even no parameters at all, like this:

  • () => Console.Write("Hi Lambda"); 


So now that you have a basic idea about Lambda expressions, it’s better to move on to a few more examples:

Func<int, int> f1 = x => { return x + 2; };


Produces the output: - 3.

As I mentioned first this is actually nothing more than an anonymous method, so this can be written in as:

Func<int, int> f1 = delegate(int x) { return x + 2; };


Produces the output: - 3.


We can even write methods of several lines, although it is not what we expecting by using Lambda expression, it is possible to write and is perfectly correct.

Func<int, double, string, int> f4 = (x, y, z) => 
    int i_1 = x; 
    int i_2 = Convert.ToInt32(y); 
    int i_3; 
    if (!int.TryParse(z, out i_3)) 
        return -1; 
        return i_1 + i_2 + i_3; 
Console.WriteLine(f4(1, 2.0, "3"));


Another useful example:

button1.Click += (s, ea) => MessageBox.Show("Hi");


Here I have given you an introduction and some examples where Lambda expression becomes helpful, easy to use. C# 3.0 has made developers' life more easy.


  • 3rd April, 2008: Initial post


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


About the Author

No Biography provided

You may also be interested in...


Comments and Discussions

QuestionThis lambda expression doesn't print Hello on console Pin
NIKS SHINE28-Jun-13 21:13
memberNIKS SHINE28-Jun-13 21:13 
GeneralClean, simple and useful Pin
Jacek Gajek13-Dec-09 2:29
memberJacek Gajek13-Dec-09 2:29 
CP members vote 1 because they already know all of this.
The article is a good intro to lambda expressions because it is short and simple.
Most collegues of mine don't read articles that are more than three pages.
I suggest adding an example of usisng lambda expression as a comparison function in list.Sort((a,b)=>a-b);.

Greetings - Jacek

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161018.1 | Last Updated 3 Apr 2008
Article Copyright 2008 by Udeshika Horanagama
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid