委托的定义

委托的解释:可以赋值一个方法的引用,通过委托变量来调用这个方法

委托的定义方法:delegate 方法签名(T)    ps:方法的签名=返回值+方法名+参数

                             委托声明的变量只能指向相同返回值和相同的参数类型。

private delegate string GetAString();

static void Main(){
	int x = 40;
	GetAString firstStringMethod = new GetAString(x.ToString);//第一种赋值方式(初始化)
GetAString firstStringMethod =x.ToString //第二种赋值方式(初始化)
String s=
firstStringMethod();//第一种调用方式
String s=firstStringMethod.Invoke//第二种调用方式
Console.WriteLine(s); }

  内置的Action委托

       无返回值有参数Action<T> T的最大个数为16为多种类型

       内置Func委托

      可有返回值和参数 Func<T1,T2> 前面的T1代表参数类型最多为16个,后面的T2为返回值类型。

private delegate string GetAString();
static void Main(){int x = 40;GetAString firstStringMethod = new GetAString(x.ToString);Console.WriteLine(firstStringMethod());}

原文地址:https://www.cnblogs.com/jiangxiaoming/p/12985615.html