C# 中的泛型委托是什么?

2023年 8月 29日 36.0k 0

C# 中的泛型委托是什么?

delegate T myDelegete(T n);

示例

以下示例展示了如何在 C# 中创建通用委托 -

using System;
using System.Collections.Generic;

delegate T myDelegete(T n);
namespace GenericDelegateAppl {
class TestDelegate {
static int num = 5;

public static int AddNum(int p) {
num += p;
return num;
}

public static int MultNum(int q) {
num *= q;
return num;
}

public static int getNum() {
return num;
}

static void Main(string[] args) {
//create delegate instances
NumberChanger nc1 = new NumberChanger(AddNum);
NumberChanger nc2 = new NumberChanger(MultNum);

//calling the methods using the delegate objects
nc1(50);
Console.WriteLine("Value of Num: {0}", getNum());

nc2(10);
Console.WriteLine("Value of Num: {0}", getNum());
Console.ReadKey();
}
}
}

登录后复制

以上就是C# 中的泛型委托是什么?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论