6. 第二章:C#委托和事件之.net framework3.5委托扩展

第二章:C#委托和事件

 

第三节:Action<T>和Func<T,TResult>

这些都是在.net framework 3.5新增的委托类型,在.net framework 2.0里面,我们用的委托是Delegate

过多的理论叙述不是我们的目的,这里我们用示例的方式展示给大家看一看这些新增的委托的用法:(涉及到函数的重载和泛型的知识点,C#基础不在此处赘述。)

1. Action / Action<T> / Action<T,S> / Action<T,S,M> / ....(无参和多个泛型传参均可),[Action无返回值函数!]

 1     public class MyClass2<T>
 2     {
 3         //无传参
 4         private Action _action1;
 5         //一个传参
 6         private Action<T> _action2;
 7 
 8         public MyClass2(Action action1, Action<T> action2)
 9         {
10             this._action1 = action1;
11             this._action2 = action2;
12         }
13 
14         public Action Action1
15         {
16             get
17             {
18                 return this._action1;
19             }
20         }
21 
22         public Action<T> Action2
23         {
24             get
25             {
26                 return this._action2;
27             }
28         }
29     }
 1     public class MyClass2<T, S> : MyClass2<T>
 2     {
 3         //两个传参
 4         private Action<T, S> _action3;
 5 
 6         public MyClass2(Action action1, Action<T> action2, Action<T, S> action3)
 7             : base(action1, action2)
 8         {
 9             this._action3 = action3;
10         }
11 
12         public Action<T, S> Action3
13         {
14             get
15             {
16                 return this._action3;
17             }
18         }
19     }

调用:

 1         static void Main(string[] args)
 2         {
 3             //两个参数,泛型为string类型
 4             MyClass2<string> myClass2_001 = new MyClass2<string>(MyMethod, MyMethod);//第一个MyMethod是函数1,第二个MyMethod是函数2
 5             myClass2_001.Action1.Invoke();
 6             myClass2_001.Action2.Invoke("Microsoft Visual Studio 2013");
 7             Console.WriteLine();
 8 
 9             //两个参数,泛型为int类型
10             MyClass2<int> myClass2_002 = new MyClass2<int>(MyMethod, MyMethod);//第一个MyMethod是函数1,第二个MyMethod是函数3(若没有函数3,则会调用函数4; 若有函数3,则会选择函数3确定的类型而不选择函数4)
11             myClass2_002.Action1.Invoke();
12             myClass2_002.Action2.Invoke(100);
13             Console.WriteLine();
14 
15             //两个参数,泛型为实例化才定义类型
16             MyClass2<double> myClass2_003 = new MyClass2<double>(MyMethod, MyMethod);//第一个MyMethod是函数1,第二个MyMethod是函数4
17             myClass2_003.Action1.Invoke();
18             myClass2_003.Action2.Invoke(3.1415926);
19             Console.WriteLine();
20 
21             //三个参数,泛型为实例化才定义类型
22             MyClass2<double, string> myClass2_004 = new MyClass2<double, string>(MyMethod, MyMethod, MyMethod);//第一个MyMethod是函数1,第二个MyMethod是函数4,第三个MyMethod是函数5
23             myClass2_004.Action1.Invoke();
24             myClass2_004.Action2.Invoke(3.1415926);
25             myClass2_004.Action3.Invoke(3.1415926, "Microsoft Visual Studio 2013");
26             Console.WriteLine();
27 
28             //Lambda语法的匿名函数,注意对比这种写法和前面的写法有何不同!
29             MyClass2<DateTime> myClass2_005 = new MyClass2<DateTime>(() =>
30             {
31                 Console.WriteLine("Written by lambda: Hello, world!");
32             }, (param) =>
33             {
34                 Console.WriteLine(string.Format("Written by lambda: {0}", param));
35             });
36             myClass2_005.Action1.Invoke();
37             myClass2_005.Action2.Invoke(System.DateTime.Now);
38 
39             Console.ReadLine();
40         }
41 
42         //函数1
43         private static void MyMethod()
44         {
45             Console.WriteLine("Hello, world!");
46         }
47 
48         //函数2
49         private static void MyMethod(string param)
50         {
51             Console.WriteLine(param);
52         }
53 
54         //函数3
55         private static void MyMethod(int param)
56         {
57             Console.WriteLine(param);
58         }
59 
60         //函数4
61         private static void MyMethod<T>(T param)
62         {
63             Console.WriteLine(param);
64         }
65 
66         //函数5
67         private static void MyMethod<T, S>(T param1, S param2)
68         {
69             Console.WriteLine(param1);
70             Console.WriteLine(param2);
71         }

运行结果:

Hello, world!

Microsoft Visual Studio 2013

Hello, world!

100

Hello, world!

3.1415926

Hello, world!

3.1415926

3.1415926

Microsoft Visual Studio 2013

Written by lambda: Hello, world!

Written by lambda: 2016/1/3 2:21:51(这里的时间不一定是这个,根据你自己的运行时间为准!)

2. Func<T,TResult> / Func<T,S,TResult> /...(支持多个泛型参数传入,但一定至少要有一个传入参数,而且返回TResult只能有一个!)

示例1里面已经用了函数的重载和泛型的方式去展现,那在此处的示例就不再用这些方式去展示了,原理类似。(如果想要扩展请自己动手,若写不出就说明阁下基本功掌握的还未熟练,多加练习吧~)

 1     public class MyClass3
 2     {
 3         private Func<string, string> _func;
 4 
 5         public MyClass3(Func<string, string> func)
 6         {
 7             this._func = func;
 8         }
 9 
10         public Func<string, string> Func
11         {
12             get
13             {
14                 return this._func;
15             }
16         }
17     }

调用:

 1         static void Main(string[] args)
 2         {
 3             MyClass3 myClass3_001 = new MyClass3(MyMethod);
 4             Console.WriteLine(myClass3_001.Func.Invoke("Jacky"));
 5             Console.WriteLine(myClass3_001.Func.Invoke("Tom"));
 6 
 7             Console.ReadLine();
 8         }
 9 
10         private static string MyMethod(string param)
11         {
12             return string.Format("Good morning, {0}!", param);
13         }

运行结果:
Good morning, Jacky!
Good morning, Tom!

原文地址:https://www.cnblogs.com/Min-min/p/5095730.html