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);
        }

 

posted @ 2017-06-26 08:11  田云  阅读(174)  评论(0编辑  收藏  举报