首页 > 代码库 > C#匿名方法

C#匿名方法

首先,介绍匿名方法的发展史,以代码展示:

static void Main(string[] args)        {            //最原始的委托            TestDelegate testDelA = new TestDelegate(Method);            //C# 2.0 后 Anonymous Method            TestDelegate testDelB = delegate(string s) { Console.WriteLine(s); };            //C# 3.0 lambda            TestDelegate testDelC = (s) => { Console.WriteLine(s); };            testDelA("Hello, this is a delegate!");            testDelB("This is a anonymous Method!");            testDelC("This is a lambda Method");            Console.ReadLine();        }        static void Method(string s)        {            Console.WriteLine(s);        }

 

C#匿名方法