Tuesday, December 29, 2009

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 <Employee> myEmployees = GetAllEmployees();

We will use System.Collection.List<T>.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.


We will now find the employee with the employee name filter.
List <Employee> employee = myEmployees.FindAll( new Predicate<Employee>(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> 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> 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.

0 Comments:

blogger templates | Make Money Online