zoukankan      html  css  js  c++  java
  • c#中的泛型委托(@WhiteTaken)

    今天学习一下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中搜索元素。(没有用到过,等用到了再更新)

  • 相关阅读:
    Log4Net学习【三】
    Log4Net学习【二】
    Log4Net学习【一】
    Asp.Net生命周期系列六
    Asp.Net生命周期系列五
    Asp.Net生命周期系列四
    Asp.Net生命周期系列三
    我为什么很烦在DB服务器上安装杀毒软件
    SQL Server 2012故障转移的looksalive check和is alive check
    如何让用户只能访问特定的数据库(MSSQL)
  • 原文地址:https://www.cnblogs.com/WhiteTaken/p/6293063.html
Copyright © 2011-2022 走看看