.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> .Net Framework >> Post New Resource Bookmark and Share   

 Subscribe to Articles

C# 3.0 new Features - Part 2

Posted By:Kumar       Posted Date: December 29, 2009    Points: 25    Category: .Net Framework    URL: http://www.dotnetspark.com  
 

C# 3.0 new Features - Part 2 . 

Continuing from part 1, here I will go one more step further and explain about Lambda Expression basics in C# 3.0 which will help us in writing a LINQ query.

Lambda Expressions -  Part 1


In C# 2.0 we used to write anonymous delegate method that helped us in minimizing the code writing.

Example: The below code snippet that searches for a Employee based on the Empname field using predicates in C# 2.0

class Employee
    {
        private string empName;
        public string EmpName
        {
            get
            {
                if (empName != null)
                {
                    return empName;
                }
            }
            set
            {
                empName = value;
            }
        }
    }

Now we will get the employee details from GetAllEmployees() function which is written in Data Access Layer (DAL) Layer.

List  myEmployees = GetAllEmployees();

We will use System.Collection.List.FindAll to search for the Employee based on EmpName.

MSDN has following information on the FindAll method.

Retrieves all the elements that match the conditions defined by the specified predicate.

For more information : http://msdn.microsoft.com/en-us/library/fh1w7y8z.aspx

We will now find the employee with the employee name filter.

List  employee = myEmployees.FindAll( new Predicate(EmpNameFilter));
//Defination for EmpNameFilter
static bool EmpNameFilter(Employee e)
{
            e.EmpName = "Kumar"
}

We will rewrite the above code using C# 2.0 Anonymous method.
List  employee = myEmployees.FindAll(delegate(Employee e) {return e.EmpName="Kumar"})

We have reduced some coding steps with the help of anonymous method, we still able to reduce the coding steps with the help of Lambda Expression

List  employee = myEmployees.FindAll(e=>e.EmpName=="Kumar");

So basically when we have a lambda expression defined then we are inherently using delegates. Its bit difficult for the first time but over a period of time we will feel happy using it.

Accoding to MSDN the following information for Lambda Expression.

A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.

All lambda expressions use the lambda operator =>, which is read as "goes to". The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is read "x goes to x times x." This expression can be assigned to a delegate type as follows:

delegate int del(int i);
static void Main(string[] args)
{
    del myDelegate = x => x * x;
    int j = myDelegate(5); //j = 25
}

The => operator has the same precedence as assignment (=) and is right-associative.
Lambdas are not allowed on the left side of the is or as operator.

Hope this article helps you to understand the basic about Lambda Expression.

Stay tuned for Lambda Expression Part 2.

Happy Coding and Leaning.
Kumar
 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend