Partager via


C# 3.0 : Exploring Lambda Expression

I started playing with Lambda expression after the TechEd demo by Anders Hejlsberg. Couple of nice things I would like to share with you.

 

As there might be many definitions for Lambda expression but to me Lambda Expression is the concise way to write functional implementation for Anonymous Method. This is been used by compiler to translate LINQ to method calls. This also allows us to maintain 100% backward compatibility with any managed version of C#.

 

Lambda function can be created using the Generic delegate Func. Func<A,R> (represents a function taking an argument of type A and returning a value of type R) is the predefined .NET call to a delegate for n number of parameters with any type. Life is easy for us. Func is defined inside System.Linq namespace. So being developer we do not have to bother about the number of variable and there types. We can simply go ahead and create any function using Func.

 

Now if you have an anonymous method for a List of Integers which finds the even numbers from the list.

 

//Generic List of Integers

List<int> arrInt = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

//Using delegate (anonymous method) get the even numbers

List<int> even1 = arrInt.FindAll(delegate(int i2) { return i2 % 2 == 0; });

//dump them in the console

foreach (int j in even1)

{

    Console.WriteLine(j);

}

Now, if I want to implement Lambda Expression there the code will look like,

 

//Generic List of Integers

List<int> arrInt = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

//Using Lambda Expression get the even numbers

List<int> even1 = arrInt.FindAll(i => i % 2 ==0);

//dump them in the console

foreach (int j in even1)

{

    Console.WriteLine(j);

}

 

Now, if you want to create the same list by using Lambda Expression and Func

//This means I am creating a function which

//takes an argument integer and returns bool.

Func<int, bool> EvenGetter = x => x % 2 == 0;

//Generic List of Integers

List<int> arrInt = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

//Using Lambda Expression get the even numbers

IEnumerable<int> even1 = arrInt.Where(EvenGetter);

//dump them in the console

foreach (int j in even1)

{

    Console.WriteLine(j);

}

 

So here we are reusing a Function with the name EvenGetter and this is like any other function not in embedded.

 

Func is very powerful, we can create List of Lambda Expression Functions and iterate through the list and check one input through multiple functions,

 

Let us assume that I need to pass one integer and get some four out puts. I will create Generic List of Func’s which takes one argument as double and returns double.

 

List<Func<double, double>> funcs = new List<Func<double,double>>();

//Add function to the list

funcs.Add(x => x * x); //Get the square

funcs.Add(x => 1 / x);

funcs.Add(x => Math.Sqrt(x)); //Sqr root of x

//iterate through the list

foreach (var f in funcs)

{

    //Execute the functions one by one with the value 100

    Console.WriteLine(f(100));

}

 

 

Output

---------

10000

0.01

10

Press any key to continue . . .

 This is pure functional programming.

Namoskar!!!

Comments

  • Anonymous
    November 29, 2007
    I started playing with Lambda expression after the TechEd demo by Anders. Couple of nice things I would

  • Anonymous
    November 29, 2007
    This is fascinating. I need to figure out a practical way to use this sort of stuff. Do you know what method performs the best (memory/CPU)?

  • Anonymous
    November 29, 2007
    Link Listing - November 29, 2007

  • Anonymous
    November 29, 2007
    ASP.NET Testing ScottGu: Alternate View Engines with ASP.NET MVC (NVelocity) [Via: Chad Myers ] Link...

  • Anonymous
    November 30, 2007
    @Sam Lambda is the best because it is optimized as it has Table Lookup Feature by default.

  • Anonymous
    November 30, 2007
    Slight correction Func is not a keyword but a set of delegates in System.LINQ namespace.

  • Anonymous
    November 30, 2007
    Slight correction Func is not a keyword but a set of delegates in System.LINQ namespace.

  • Anonymous
    December 03, 2007
    @Josh, Modified, thanks for the correction. WRIJU

  • Anonymous
    December 17, 2007
    .NET:C#3.0:ExploringLambdaExpressionWhat'sAilingASP.NETWebFormsAlternateViewEngineswi...

  • Anonymous
    February 10, 2008
    Thanks for sharing!  Very interesting.