C# 语法特性

概述

   匿名方法的本质其实就是委托

   编译后会生成委托对象,生成方法,然后把方法装入委托对象,最后赋值给声明的委托变量。

   (匿名方法可以省略参数:编译的时候会自动为这个方法按照委托签名的参数添加参数)

实例:

    public delegate void MyConsoleWrite(string strMsg);
            
        void WriteMsg(string s)
        {
          Console.WriteLine(s);
        }

        //匿名方法测试
        MyConsoleWrite delMCW1 = new MyConsoleWrite(WriteMsg);
        delMCW1("天下第一");

        MyConsoleWrite delMCW2 = delegate (string strMsg)
        {
          Console.WriteLine(strMsg);
        };
        delMCW2("天下第二");
原文地址:https://www.cnblogs.com/zhangchaoran/p/8780250.html