Welcome to MSDN Blogs Sign in | Join | Help

Wriju's BLOG

.NET and everything
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!!!

Posted: Thursday, November 29, 2007 5:19 PM by wriju

Comments

Noticias externas said:

I started playing with Lambda expression after the TechEd demo by Anders. Couple of nice things I would

# November 29, 2007 12:39 PM

Sam said:

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)?

# November 30, 2007 12:26 AM

Christopher Steen said:

Link Listing - November 29, 2007

# November 30, 2007 12:28 AM

Christopher Steen said:

ASP.NET Testing ScottGu: Alternate View Engines with ASP.NET MVC (NVelocity) [Via: Chad Myers ] Link...

# November 30, 2007 12:28 AM

wriju said:

@Sam

Lambda is the best because it is optimized as it has Table Lookup Feature by default.

# November 30, 2007 10:50 AM

Josh said:

Slight correction Func is not a keyword but a set of delegates in System.LINQ namespace.

# November 30, 2007 2:15 PM

Josh said:

Slight correction Func is not a keyword but a set of delegates in System.LINQ namespace.

# November 30, 2007 2:16 PM

wriju said:

@Josh,

Modified, thanks for the correction.

WRIJU

# December 3, 2007 5:34 PM

gOODiDEA said:

.NET:C#3.0:ExploringLambdaExpressionWhat'sAilingASP.NETWebFormsAlternateViewEngineswi...

# December 17, 2007 10:36 PM

irek said:

Thanks for sharing!  Very interesting.

# February 10, 2008 12:48 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

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

Page view tracker