
使用通用委托,您不需要定义委托语句。它们在系统命名空间中定义。
您可以使用类型参数定义通用委托。例如 –
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# 中的泛型委托是什么?的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1434973.html
微信扫一扫
支付宝扫一扫