.net 委托

1.1 定义
委托是一种引用方法的类型。一旦为委托分配了方法,委托将与该方法具有完全相同的行为。委托方法的使用可以像其他任何方法一样,具有参数和返回值,如下面的例子所示

 public delegate void VoidDeletage(int a, int b);

与委托的签名(由返回类型和参数组成)匹配的任何方法都可以分配给该委托。
1.2 委托使用
使用委托的四部曲:

  1. 定义一种委托类型
  2. 委托执行时要调用方法
  3. 定义一个委托实例
  4. 委托实例的调用

接下来,按照四部曲进行实战
首先,定义一个没有返回参数的委托,传入a,b两个参数

        private delegate void VoidDeletage(int a, int b);

其次,创建委托执行时的调用方法

        private static void Add(int a, int b)
        {
            Console.WriteLine($"使用Add方法定义delegate:{a}+{b}={a + b}");
        }

创建委托实例

VoidDeletage voidDeletage = new VoidDeletage(Add);

实例调用

 voidDeletage(1, 1);

同时,我们输出VoidDelegate的类型

Console.WriteLine("委托是否为Class:" + voidDeletage.GetType().IsClass);

运行得到结果
在这里插入图片描述
从输出结果可以看出,委托是一种特殊的类。
通过.net 提供的语法糖可以更快定义实例化委托以及定义执行时所调用方法

 VoidDeletage voidDeletage = (a, b) => Console.WriteLine($"使用lambda表达式定义delegate:{a}+{b}={a + b}");

1.3 Action<>与Func<>
我们通过查看原始定义,可以看出Action<>、Func<>本质上就是委托

namespace System
{
    //
    // 摘要:
    //     Encapsulates a method that has no parameters and does not return a value.
    public delegate void Action();
}
namespace System
{
    //
    // 摘要:
    //     Encapsulates a method that has no parameters and returns a value of the type
    //     specified by the TResult parameter.
    //
    // 类型参数:
    //   TResult:
    //     The type of the return value of the method that this delegate encapsulates.
    //
    // 返回结果:
    //     The return value of the method that this delegate encapsulates.
    public delegate TResult Func<out TResult>();
}

所以同样可以使用委托四部曲的步骤进行使用,因为Action<>、Func<>是.net已经定义好的委托,所以我们可以省略掉四部曲中的委托定义步骤

	// 使用定义好的方法实例Action委托
	Action<int, int> add = new Action<int, int>(Add);
	add(1, 1);

	// 使用.net提供的语法进行实例
	Action<int, int> add = (a, b) => Console.WriteLine($"使用lambda表达式定义delegate:{a}+{b}={a + b}");
	add(1, 1);

通过Action<in T,in T>原始定义的查看,可以看出Action<>委托是没有返回值的

namespace System
{
    //
    // 摘要:
    //     Encapsulates a method that has two parameters and does not return a value.
    //
    // 参数:
    //   arg1:
    //     The first parameter of the method that this delegate encapsulates.
    //
    //   arg2:
    //     The second parameter of the method that this delegate encapsulates.
    //
    // 类型参数:
    //   T1:
    //     The type of the first parameter of the method that this delegate encapsulates.
    //
    //   T2:
    //     The type of the second parameter of the method that this delegate encapsulates.
    public delegate void Action<in T1, in T2>(T1 arg1, T2 arg2);
}

同理,我们来定义Func<>委托

	// 使用定义好的方法实例Action委托
	Func<int, int, int> add = new Func<int, int, int>(Add);
	int result = add(1, 1);

	// 使用.net提供的语法进行实例
	Func<int, int, int> add = (a, b) => a + b);
	add(1, 1);

	private static int Add(int a, int b)
    {
    	Console.WriteLine($"使用Add方法定义delegate:{a}+{b}={a + b}");
    	return a + b;
    }

查看原始定义,我们可以看到,Func<>委托是有返回值的

namespace System
{
    //
    // 摘要:
    //     Encapsulates a method that has two parameters and returns a value of the type
    //     specified by the TResult parameter.
    //
    // 参数:
    //   arg1:
    //     The first parameter of the method that this delegate encapsulates.
    //
    //   arg2:
    //     The second parameter of the method that this delegate encapsulates.
    //
    // 类型参数:
    //   T1:
    //     The type of the first parameter of the method that this delegate encapsulates.
    //
    //   T2:
    //     The type of the second parameter of the method that this delegate encapsulates.
    //
    //   TResult:
    //     The type of the return value of the method that this delegate encapsulates.
    //
    // 返回结果:
    //     The return value of the method that this delegate encapsulates.
    public delegate TResult Func<in T1, in T2, out TResult>(T1 arg1, T2 arg2);
}

通过对比我们可以看出,Action<>与Func<>本质上都是委托,他们唯一的区别在于,有没有返回值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值