Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Mindcracker MVP Summit 2012
Search :       Advanced Search »
Home » C# Language » Lambda Expression in c# 3.0

Lambda Expression in c# 3.0

This article will explain about Lambda expression and its usage. This article will also give differences between Lambda expression and Anonymous method. This article will talk about type inference in Lambda expression. This will also talk about delegate type conversion of lambda expression.

Author Rank :
Page Views : 12016
Downloads : 141
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
DelegateTesting.zip
 
 
Nevron Chart
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


This article will explain about Lambda expression and its usage. This article will also give differences between Lambda expression and Anonymous method. This article will talk about type inference in Lambda expression. This will also talk about delegate type conversion of lambda expression.

We will start with a code. This is a very simple code which is printing Hello World. But this is done with help of Lambda Expression.

image1.gif

  1. Lambda Expression provides a concise functional syntax for writing anonymous method.
  2. It is a type safe way to write functions that can be passed as argument for subsequent evaluation.
  3. Lambda expression are superset of anonymous method.
  4. It can contain either and statement or an expression.
  5. It may have or may not have parameters.
  6. Its parameter could be implicitly or explicitly type.

    image2.gif
  7. It uses the lambda operator. => This operator is known as GOES TO 

    image3.gif
     
  8. Different lambda expressions are

    image4.gif
     
  9. There are two types of Lambda expression

    image5.gif

    Expression Lambda

    A lambda expression, which is having expression at right hand side is called Expression lambda.

    image6.gif

    Examples:

    image7.gif

    Statement Lambda

    A lambda expression, which is having statement at right hand side is called Statement lambda.

    image8.gif

    Examples:

    image9.gif

Parameter types in Lambda Expression

image10.gif

  1. For explicit type parameter, each parameter does have their type.

    image11.gif
     
  2. For implicit type parameter, type of parameter is inferred from compatible delegate type.

    image12.gif

Working Example

See, the below code. How lambda expression is being used to find out name of the authors having more than 100 articles.

image13.gif

Output

image14.gif

Lambda Expression conversion to Compatible delegate type
Lambda expression is considered as a value, which is having special conversion rules. The Lambda expression value does not have a type but can be converted to compatible delegate type.

Let us suppose,

D-> Delegate

L-> Lambda Expression

Then D and L are called compatible if

  1. D and L have same number of parameters.
  2. If L has explicit type parameter then L should also have same type parameter list.
  3. If L has implicit type parameter then L should not have Ref and Out parameters.
  4. If D has a void return type and L is an expression type lambda expression then body of L is a valid expression and considered as a statement expression.
  5. If D has a void return type and L is a statement type lambda expression then body of L is a valid statement block where no expression contains return statement.
  6. If D is returning some type and L is an expression type lambda expression then body of L is a valid expression, if that is implicitly convertible to the return type of D.
  7. If D is returning some type and L is a statement lambda expression then body of L is a valid statement body if in which each Return statement specifies the type which could be implicitly convertible to the return type of D.

Example
Let us suppose, there is a delegate

delegate void MyDelegate(int num);

Below lambda expression would work fine due to rule 5 stated above.


MyDelegate r = (num) => Console.WriteLine(num) ;

Below lambda expression would not work fine due to failure of rule 5 stated above.

image15.gif

Lambda Expression and Anonymous method

  1. Lambda Expression permits parameter type to be omitted and inferred whereas anonymous method requires parameter type to be explicitly stated.
  2. The body of lambda expression can be expression or statement whereas body of anonymous method only could be statement.
  3. Lambda Expression within an expression body can be converted to expression tree.

Type inference in Lambda Expression

Let, we suppose we are assigning a value to a variable like below. Compiler will report an error "Cannot assign lambda expression to an implicitly typed local variable". Because c# type inference cannot be used to infer to infer type of a lambda expression.

image16.gif

This could be solved by, explicitly declaring the type of the lambda expression.

Func<int> res = () => 9;

For inferred return type of a lambda expression L is determined as follows:

  • If L is an expression type Lambda Expression, the type of that expression is the inferred return type of L.
  • If L is a statement type Lambda Expression, if the set formed by the types of the expressions in the block's return statements contains exactly one type to which each type in the set is implicitly convertible, and if that type is not the null type, then that type is the inferred return type of L.
  • Otherwise, a return type cannot be inferred for L.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Dhananjay Kumar
I write few articles on Microsoft technologies. Read my blog at . | Mincracker MVP | Microsoft MVP
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Chart
Become a Sponsor
 Comments
expression tree by joanne On October 18, 2009
i would like to ask some sample codes of an expression tree using c#...
Reply | Email | Modify 
Mindcracker MVP Summit 2012
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.