委托是一种可用于封装命名或匿命名方法的引用类型
委托是一种类,从代码上,我个人理解为给“规定的”方法(静态方法,或是某个类的成员函数)重命名,即被重命名的方法并不是随意的。
被委托的方法与委托的返回值类型与传递参数要一致
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace delegation01
{
class Program
{
public delegate void mydelegate(string mydelegate); //委托声明
class method
{
public void display(string name) //该方法为Method类的成员函数
{
Console.WriteLine("委托人" + name);
}
}
static void display(string name) //该方法为静态方法
{
Console.WriteLine("委托人" + name);
}
static void Main(string[] args)
{
method dis = new method();
mydelegate a = new mydelegate(dis.display); //受委托的方法要么是某个类的成员,要么是静态方法
mydelegate b = new mydelegate(display);
a("A");
b("B");
}
}
}
