 
            顾名思义,匿名方法就是没有名字的方法。 C# 中的匿名方法可以使用委托关键字定义,并且可以分配给委托类型的变量。
using System;
namespace ConsoleApp1
{
    public delegate void Print(int value);
    class Program
    {
        static void Main(string[] args)
        {
            Print print = delegate (int val)
            {
                Console.WriteLine("在匿名方法里面. Value: {0}", val);
            };
            print(100);// 在匿名方法里面. Value: 100
        }
    }
}
匿名方法可以访问在函数外中定义的变量。
using System;
namespace ConsoleApp1
{
    public delegate void Print(int value);
    class Program
    {
        static void Main(string[] args)
        {
            int i = 10;
            Print print = delegate (int val)
            {
                val += i;
                Console.WriteLine("在匿名方法里面. Value: {0}", val);
            };
            print(100);// 在匿名方法里面. Value: 110
        }
    }
}
匿名方法也可以传递给带有委托参数的方法。
在以下示例中, PrintHelperMethod() 接受 Print 委托的第一个参数:
using System;
namespace ConsoleApp1
{
    public delegate void Print(int value);
    class Program
    {
        public static void PrintHelperMethod(Print printDel, int val)
        {
            val += 10;
            printDel(val); // 匿名方法: 110
        }
        static void Main(string[] args)
        {
            PrintHelperMethod(delegate (int val) { Console.WriteLine("匿名方法: {0}", val); }, 100);
        }
    }
}
输出
匿名方法: 110
匿名方法可以用作事件处理程序:
    class Program
    {
        public static event Action<int> changed;
        static void Main(string[] args)
        {
            //注册事件处理程序同常是在其它类里面,这边只是演示匿名方法
            changed += delegate (int i) { Console.WriteLine($"事件内:{i}"); };
            changed += delegate (int i) { Console.WriteLine($"第二个事件内:{i}"); };
            changed(10); 
            //事件内:10 
            //第二个事件内:10
        }
    }
输出
事件内:10
第二个事件内:10
C# 3.0 引入了 lambda 表达式,它也像匿名方法一样工作。