c#中的泛型委托详解

所属分类: 软件编程 / C#教程 阅读数: 67
收藏 0 赞 0 分享

今天学习一下c#中的泛型委托。

1.一般的委托,delegate,可以又传入参数(<=32),声明的方法为  public delegate void SomethingDelegate(int a);

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace delegateSummary {
 public delegate void GetIntDelegate(int a); //声明一个委托
 public class getIntClass { 
 public static void SetDelegateString(int a,GetIntDelegate getIntDelegate) {//使用委托
  getIntDelegate(a);
 }
 public void getInt1(int a) { //方法1
  Console.WriteLine("getInt1方法调用,参数为:" + a);
 }
 public void getInt2(int a) { //方法2
  Console.WriteLine("getInt2方法调用,参数为:" + a);
 }
 }
 class Program {
 static void Main(string[] args) {
  getIntClass gc=new getIntClass();
  getIntClass.SetDelegateString(5, gc.getInt1);  //方法1,2作为委托的参数
  getIntClass.SetDelegateString(10, gc.getInt2); 
  Console.WriteLine("=====================");
  GetIntDelegate getIntDelegate;
  getIntDelegate = gc.getInt1;     //将方法1,2绑定到委托
  getIntDelegate += gc.getInt2;
  getIntClass.SetDelegateString(100, getIntDelegate); 
  Console.Read();
 } 
 }
}

输出结果,注意两种方式的不同,第一种将方法作为委托的参数,第二种是将方法绑定到委托。

2.泛型委托之Action,最多传入16个参数,无返回值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace delegateSummary {
 class Program {
 static void Main(string[] args) {
  TestAction<string>(getString, "WhiteTaken"); //传入方法
  TestAction<int>(getInt, 666);
  TestAction<int, string>(getStringAndInt, 666, "WhiteTaken");
  Console.Read();  
 }
 public static void TestAction<T>(Action<T> action,T p1) {        //Action传入一个参数测试
  action(p1);
 }
 public static void TestAction<T, P>(Action<T, P> action, T p1, P p2) { //Action传入两个参数测试
  action(p1,p2);
 }
 public static void getString(string a) {    //实现int类型打印
  Console.WriteLine("测试Action,传入string,并且传入的参数为:" +a);
 }
 public static void getInt(int a) {     //实现String类型打印
  Console.WriteLine("测试Action,传入int,并且传入的参数为:" + a);
 }
 public static void getStringAndInt(int a, string name) {    //实现int+string类型打印
  Console.WriteLine("测试Action,传入两参数,并且传入的参数为:" + a+":"+name);
 }
 }
}

测试结果:

3.泛型委托之Func,最多传入16个参数,有返回值。(写法与Action类似,只是多了返回值)

4.泛型委托之predicate(不是很常用),返回值为bool,用在Array和list中搜索元素。(没有用到过,等用到了再更新)

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!

更多精彩内容其他人还在看

C# SendInput 模拟鼠标操作的实现方法

C# SendInput 模拟鼠标操作的实现方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#中 paint()与Onpaint()的区别

paint是事件onpaint方法onpaint方法是调用paint事件的,用哪一个,效果是一样,就看那一个方便了内部是这样实现的:
收藏 0 赞 0 分享

c#中GetType()与Typeof()的区别

c#中GetType()与Typeof()的区别,需要的朋友可以参考一下
收藏 0 赞 0 分享

将字符串转换成System.Drawing.Color类型的方法

将字符串转换成System.Drawing.Color类型的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# 抓取网页内容的方法

C# 抓取网页内容的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

基于C#后台调用跨域MVC服务及带Cookie验证的实现

本篇文章介绍了,基于C#后台调用跨域MVC服务及带Cookie验证的实现。需要的朋友参考下
收藏 0 赞 0 分享

使用C#获取远程图片 Form用户名与密码Authorization认证的实现

本篇文章介绍了,使用C#获取远程图片 Form用户名与密码Authorization认证的实现。需要的朋友参考下
收藏 0 赞 0 分享

Winform跨线程操作的简单方法

线程间操作无效:从不是创建控件“label1”的线程访问它
收藏 0 赞 0 分享

C# WINFORM 强制让窗体获得焦点的方法代码

C# WINFORM 强制让窗体获得焦点的方法代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#中方括号[]的语法及作用介绍

C#中方括号[]可用于数组,索引、属性,更重要的是用于外部DLL类库的引用。
收藏 0 赞 0 分享
查看更多