委托

——委托:把方法参数化

1、 关键字:delegate  写在类的外面

2、函数签名:返回的类型,参数  要保持一致       定义委托时根据函数来写

public delegate int first(int a,int b)

定义委托变量,指向方法

——委托不能被实例化,因为不是类

first f=new jiafa

//定义委托
public delegate void subian(string s);//写在类的外面

class Program
{
static void Main(string[] args)
{

//把方法参数化
subian s=america;

s += china;   +=  往上绑定方法    -=去掉一个方法    = 替换            事件:实际上就是一个特殊的委托
//调用语音方法
speak(s);
Console.WriteLine();
Console.ReadLine();

}


//语音功能的方法
static void speak(subian yu)//传一个函数,把整个函数作为一个参数,把方法参数化
{

yu("张三");

}

//语音包
static void america(string s)
{
Console.WriteLine(s+"hello");
}
static void china(string s)
{
Console.WriteLine(s+"你好");
}
static void hanyu(string s)
{
Console.WriteLine(s+"niyou");
}

原文地址:https://www.cnblogs.com/yp11/p/5763856.html