[C#] 委托与匿名方法

using System;
 
namespace 匿名函数
{
    class Program
    {
        delegate void TestDelegate(string s);
 
        static void M(string s)
        {
            Console.WriteLine("A参数为:{0}", s);
        }
 
        static void Main(string[] args)
        {
            //1. 委托的基本写法,及调用
            TestDelegate testDeleA = new TestDelegate(M);//通过委托的实例化将之与方法绑定
            testDeleA("hahahah");
 
            //2. 使用匿名表达式构造委托调用方法
            //C#2.0 Anonymous Method,其结构为:delegate+(各个参数)+{函数体}
            TestDelegate testDeleB = delegate (string s) { Console.WriteLine("B参数为:{0}", s); };//直接通过delegate声明将之与函数体绑定
            testDeleB("hehehehe");
            //C#3.0, 引入lamda表达法,Lambda Expression
            TestDelegate testDeleC = (x) => { Console.WriteLine("C参数为:{0}", x); };
            testDeleC("hohoho");
 
            Console.ReadKey();
        }
 
    }
}
 
 
原文地址:https://www.cnblogs.com/xihong2014/p/13898746.html