Sunday, August 29, 2010

Sample explaining Anonymous Method and Lambda Expression

ChangeInt myDelegate = new ChangeInt(
delegate(int x)
{
return x * 2;
}
);
Console.WriteLine("{0}", myDelegate(5));

Same USING LAMBDA EXPRESSION

ChangeInt myDelegate = x => x * 2;
Console.WriteLine("{0}", myDelegate(5));


If you wanted to, you could have specified the type of the argument, as follows:

ChangeInt myDelegate = (int x) => x * 2;
Console.WriteLine("{0}", myDelegate(5));

Source:-(http://www.codeproject.com/KB/cs/lambdaexpressions.aspx)

No comments: