Welcome to AspAdvice Sign in | Join | Help

From .NET Geek's Desk

Thoughts and Findings on .NET
Lambda Expressions

Before getting details of Lambda expressions let us quickly take a look into how we work with delegates and a named method since the days of .NET 1.1.Let us consider the following code sample

//Delegate declaration

public delegate void Test(string s);

class Program
       static void Main(string[] args)

           //Instantiate the delegate
           Test t = new Test(Print);

           //Invoke the delegate
           t("Hello World");

       public static void Print(string s)

This is how we work delegates and named methods.In C# 2.0 we got the feature of anonymous methods.Using anonymous methods we can directly assign a code block while instantiating a delegate as shown below:

t = delegate(string s) { Console.WriteLine(s); };

This is nothing great but a syntactical sugar coat.If you examine the IL code you will find that compiler has emitted code of another method as shown below:

.method private hidebysig static void  '<Main>b__0'(string s) cil managed
  .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
  // Code size       9 (0x9)
  .maxstack  8
  IL_0000:  nop
  IL_0001:  ldarg.0
  IL_0002:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_0007:  nop
  IL_0008:  ret
} // end of method Program::'<Main>b__0'

In the IL code of the Main method we can see the following instruction which basically loads the pointer to <Main>b__0 into stack and instantiates the delegate.

IL_0022:  ldftn      void LambdaExpression.Program::'<Main>b__0'(string)

In C# 3.0 we have a new feature , Lambda expressions which are anonymous functions with expressions and statements and can be used to instantiate the delegates as shown below:

t = (x) => { Console.WriteLine(x); };

Lambda Expressions:

  1. Use => operator called "goes to"
  2. Left hand side contains the parameter
  3. Right hand side contains the expression or the statements.

The above code block shows an implicitly typed Lambda expression.But Lambda expressions can be explicitly typed as well:

t = (string x) => { Console.WriteLine(x); };

If you check the IL code for the C# code block above you will find another compiler generated method as in case of anonymous method.Now the obvious question that comes into our mind is what is the difference between anonymous methods and lambda expression?Is it just mere difference in syntax?

The key differences between the two are (from C# 3.0 language spec):

  1. Lambda expressions allows parameter types to be omitted and inferred whereas anonymous methods require parameter types to be explicitly stated.
  2. The body of a Lambda expression can be an expression or a statement block whereas the body of an anonymous methods must be a statement block.e.g. the expression t = (int x) => { return x +2 } is same as t = (int x) => { x + 2 }
  3. Since only Lambda expressions can have an expression body, no anonymous-method-expression can be successfully converted to an expression tree type

The critical difference between the two from compiler design/implementation perspective is explained beautifully by Eric Lippert in his post:


Posted: Sunday, December 28, 2008 7:30 AM by sankarsan
Filed under: ,
Leave a Comment





Enter the code you see below

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS