委托-匿名方法

一,无参数无返回

namespace 委托
{
    class Program
    {
        public delegate void Method();
        static void Main(string[] args)
        {
            Method method = delegate ()
              {
                  Console.WriteLine("----无参数返回----");
              };
            //使用方法
            method();
            Console.Read();
        }

    }
}

二,无参数有返回

namespace 委托
{
    class Program
    {
        public delegate void Method(int a);
        static void Main(string[] args)
        {
            Method method = delegate (int a)
              {
                  Console.WriteLine($"{a}");
              };
            //使用方法
            method(10);
            Console.Read();
        }

    }
}

三,有参数有返回

namespace 委托
{
    class Program
    {
        public delegate int  Method(int a);
        static void Main(string[] args)
        {
            Method method = delegate (int a)
              {
                  return a;
              };
            //使用方法
            int result= method(10);
            Console.WriteLine(result);
            Console.Read();
        }

    }
}

  

  

猜你喜欢

转载自www.cnblogs.com/Luck1996/p/11983625.html