New Lambda Expressions in C Sharp 3.0

Hi,

One of the new features of C Sharp 3.0 is the Lambda expression. As we all know C Sharp 2.0 introduced the concept of anonymous method. An anonymous method allows code methods to be written inline instead of delegate value. A lambda expression provides a more concise way for writing anonymous methods for functional syntax.

Lambda expressions are very useful when used with (in) LINQ queries. A lambda expression provides a type safe and compact way to write which can be passed as argument for subsequent evaluation.

The syntax to write a lambda expression is very simple. The syntax of a lambda expression would be parameter list followed by => token and then the expression block that needs to be executed when the expression is invoked.

Parameter => expression


Lambda expression also allows parameter to be omitted from the declaration. When parameter type is omitted then it is inferred based on its usage. Lambda parameters are inferred at both compile time and runtime hence we also get full compile time intelligence for it. Here is an example of the lambda expression.

V => V.BlogId = 201

Lambdas are very useful in LINQ queries. Here is an example of a LINQ query using the Lambda.

var ms = from method in tStr.GetMethods()

         select method;

IEnumerable<MethodInfo> m1 = ms.Where(m => m.IsStatic == true);

Here I am using Lambda to evaluate if the method is static or not. [Please follow my previous post for more detail on LINQ query]

Thanks
Vikram


Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Posted on 9/20/2007 12:10:47 AM

I think this site is very useful,,, i will send this link to all my friends.....

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2008 Vikram Lakhotia