Tuesday, May 29, 2012

Lambda Expression in C#

Lambda expression is the new feature of C# 3.5 and it is available from vs 2008 onward. It help to iterate through the collection easily.


i => i * i

(i1, i2) => i1 + i2

parm => MessageBox.Show(
    "Do you want to save the file: " + parm + "?", 
    "Confirm file save", MessageBoxButtons.YesNo)

=> is the lambda operator which will enable the compailer to iterate through each object in the collection.
 centreDetails = centreDetailsCollection.Find(c => c.ID == 102);

here centreDetails is an object and centreDetailsCollection is its collection, ie a <T> list.
delegate int Add(int a, int b); 
public void StatementLambda(int operand1, int operan2) 
{ 
    Add result = (a, b) => 
    { 
        int c = a + b; 
        Console.WriteLine(c.ToString()); 
        return c; 
    };     result(operand1, operan2); 
}

No comments:

Post a Comment