Reference articles: http://blogs.msdn.com/b/ericwhite/archive/2006/10/03/lambda-expressions.aspx
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
namespace ConsoleApplication.LE_Delegates_An_Mthods
{
public class LambdaExpressions
{
delegate void DelWithoutReturnType(string message);
delegate string DelWithReturnType(string message, int i);
public void Sample1()
{
//Using Anonymous method
DelWithoutReturnType an = delegate(string msg) { Console.WriteLine(msg); };
//Using Method Group
DelWithoutReturnType mg = Console.WriteLine;
//Using Lambda Expression
DelWithoutReturnType le = (msg) => Console.WriteLine(msg);
an("Anonymous");
mg("Method Grp");
le("LE");
}
public void Sample2()
{
//Using Anonymous method
DelWithReturnType an = delegate(string msg, int cnt)
{
Console.WriteLine(msg);
Console.WriteLine();
return msg + cnt;
};
//Using Lambda Expression
DelWithReturnType le = (msg, cnt) =>
{
Console.WriteLine(msg);
return (msg + cnt);
};
an("Something needs to be done", 3);
le("I have changed the above delegate to Lambda Expression", 3);
}
}
}
No comments:
Post a Comment