您的位置:首页 > 其它

Some examples about how to write anonymous method and lambda expression

2011-10-31 14:13 519 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LandmaExpressionDemo
{
class Program
{
static void Main(string[] args)
{
//anonymous method
Action ac = delegate() { Console.WriteLine("hello delegate"); };
ac();

//Use Action<T> to create anonymous method with parameters
Action<string> ac2 = delegate(string strMessage) { Console.WriteLine(strMessage); };
ac2("hello world2");

//Pass the anonymous method to another method as parameter
ActionHelper(delegate(string strMessage) { Console.WriteLine(strMessage); });

//use landmar expression as parameter to another method
ActionHelper(fw => Console.WriteLine(fw));

//Use lambda expression as a delegate
Action ac3 = () => Console.WriteLine("hello ac3");
ac3();

}

public delegate void DisplayMessageDelegate(string strMesssage);

public static void ActionHelper(Action<string> ac)
{
ac("hello world3");
}

//Useful references
//1. http://msdn.microsoft.com/en-us/library/018hxwa8.aspx //2. http://msdn.microsoft.com/en-us/library/bb549151.aspx //3. http://msdn.microsoft.com/en-us/library/bb397687.aspx //4. http://msdn.microsoft.com/en-us/library/bb534960.aspx }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐