码迷,mamicode.com
首页 > Windows程序 > 详细

c#中的泛型委托(@WhiteTaken)

时间:2017-01-18 07:49:49      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:int   code   常用   tin   没有   ima   line   log   collect   

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

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

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace delegateSummary {
 8     public delegate void GetIntDelegate(int a); //声明一个委托
 9     public class getIntClass { 
10         public static void SetDelegateString(int a,GetIntDelegate getIntDelegate) {//使用委托
11             getIntDelegate(a);
12         }
13         public  void getInt1(int a) {   //方法1
14             Console.WriteLine("getInt1方法调用,参数为:" + a);
15         }
16         public void getInt2(int a) {   //方法2
17             Console.WriteLine("getInt2方法调用,参数为:" + a);
18         }
19     }
20     class Program {
21         static void Main(string[] args) {
22             getIntClass gc=new getIntClass();
23             getIntClass.SetDelegateString(5, gc.getInt1);        //方法1,2作为委托的参数
24             getIntClass.SetDelegateString(10, gc.getInt2);      
25             Console.WriteLine("=====================");
26             GetIntDelegate getIntDelegate;
27             getIntDelegate = gc.getInt1;                         //将方法1,2绑定到委托
28             getIntDelegate += gc.getInt2;
29             getIntClass.SetDelegateString(100, getIntDelegate); 
30             Console.Read();
31         }    
32     }
33 }

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

技术分享

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

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace delegateSummary {
 8     class Program {
 9         static void Main(string[] args) {
10             TestAction<string>(getString, "WhiteTaken");        //传入方法
11             TestAction<int>(getInt, 666);
12             TestAction<int, string>(getStringAndInt, 666, "WhiteTaken");
13             Console.Read();          
14         }
15         public static void TestAction<T>(Action<T> action,T p1) {           //Action传入一个参数测试
16             action(p1);
17         }
18         public static void TestAction<T, P>(Action<T, P> action, T p1, P p2) {  //Action传入两个参数测试
19             action(p1,p2);
20         }
21         public static void getString(string a) {                                //实现int类型打印
22             Console.WriteLine("测试Action,传入string,并且传入的参数为:" +a);
23         }
24 
25         public static void getInt(int a) {                                      //实现String类型打印
26             Console.WriteLine("测试Action,传入int,并且传入的参数为:" + a);
27         }
28 
29         public static void getStringAndInt(int a, string name) {               //实现int+string类型打印
30             Console.WriteLine("测试Action,传入两参数,并且传入的参数为:" + a+":"+name);
31         }
32     }
33 }

测试结果:

技术分享

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

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

 

c#中的泛型委托(@WhiteTaken)

标签:int   code   常用   tin   没有   ima   line   log   collect   

原文地址:http://www.cnblogs.com/WhiteTaken/p/6293063.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!