我们知道参数类型可以是整型、字符型、对象类型等等,当然也可以是函数。整型用int修饰,字符型用string修饰,等等。

那么参数为函数,用什么修饰?就是“委托”。

因此,可以把委托理解为函数的类型。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        public delegate int MyDelegate(int a);

        static void Main(string[] args)
        {
            DoMethod(AddOne); //AddOne和MyDelegate的参数类型和返回值类型必须保持一致
        }

        public static int AddOne(int a)
        {
            return a + 1;
        }

        public static void DoMethod(MyDelegate myDelegate)
        {
            Console.WriteLine("Result:" + myDelegate(100));
        }
    }
}

 

相关文章:

  • 2021-08-06
  • 2021-05-17
  • 2022-12-23
  • 2022-01-21
  • 2022-03-04
猜你喜欢
  • 2022-01-30
  • 2022-01-05
  • 2021-06-21
  • 2021-06-10
  • 2022-12-23
  • 2021-11-30
相关资源
相似解决方案