三、随着C#版本改变
1. C#2.0 泛型委托
C#2.0 的时候,随着泛型出现,支持了泛型委托,例如,在委托的签名中可以使用泛型,例如下面代码
public delegate string SendDelegate<T>(T message);
这样的委托适用于不同的参数类型,例如如下代码(注意使用的时候要对应具体的类型)
public delegate string SendDelegate<T>(T message); public class HR1 { public SendDelegate<string> sendDelegate1; public SendDelegate<int> sendDelegate2; public SendDelegate<DateTime> sendDelegate3; } public static class Sender1 { public static string Send1(string msg) { return ""; } public static string Send2(int msg) { return ""; } } public class Test { public void TestDemo() { HR1 hr1 = new HR1(); hr1.sendDelegate1 = Sender1.Send1; // 注意使用的时候要对应具体的类型 hr1.sendDelegate2 = new SendDelegate<int>(Sender1.Send2); hr1.sendDelegate3 = delegate (DateTime dateTime) { return dateTime.ToLongDateString(); }; } }
2. C#2.0 delegate运算符
delegate
运算符创建一个可以转换为委托类型的匿名方法:
例如上例中这样的代码:
hr1.sendDelegate3 = delegate (DateTime dateTime) { return dateTime.ToLongDateString(); };
3. C#3.0 Lambda 表达式
从 C# 3 开始,lambda 表达式提供了一种更简洁和富有表现力的方式来创建匿名函数。 使用 => 运算符构造 lambda 表达式,
例如“delegate运算符”的例子可以简化为如下代码:
hr1.sendDelegate3 = (dateTime) => { return dateTime.ToLongDateString(); };
4.C#3,NET Framework3.5,Action 、Func、Predicate
Action 、Func、Predicate本质上是框架为我们预定义的委托,在上面的例子中,我们使用委托的时候,首先要定义一个委托类型,然后在实际使用的地方使用,而使用委托只要求方法名相同,在泛型委托出现之后,“定义委托”这一操作就显得越来越累赘,为此,系统为我们预定义了一系列的委托,我们只要使用即可。
例如Action的代码如下:
public delegate void Action(); public delegate void Action<in T>(T obj); public delegate void Action<in T1, in T2>(T1 arg1, T2 arg2); public delegate void Action<in T1, in T2, in T3>(T1 arg1, T2 arg2, T3 arg3); public delegate void Action<in T1, in T2, in T3, in T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4); public delegate void Action<in T1, in T2, in T3, in T4, in T5>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5); public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6); public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7); public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8); public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9); public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10); public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11); public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12); public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13); public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14); public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, in T15>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15); public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, in T15, in T16>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16);
实际上定义了最多16个参数的无返回值的委托。
Func与此类似,是最多16个参数的有返回值的委托。Predicate则是固定一个参数以及bool类型返回值的委托。
public delegate bool Predicate<T>(T obj);
5. .NET Core 异步调用
第2.3节中,提示如下代码在.NET Core中已不支持
((SendDelegate)item).BeginInvoke(msg,null,null);
会抛出异常:
System.PlatformNotSupportedException:“Operation is not supported on this platform.”
需要异步调用的时候可以采用如下写法:
Task task = Task.Run(() => ((SendDelegate)item).Invoke(msg));
对应的 EndInvoke() 则改为: task.Wait();
5. .NET Core的 EventHandler<TEventArgs>
.NET Core 版本中,EventHandler<TEventArgs> 定义不再要求 TEventArgs 必须是派生自 System.EventArgs 的类, 使我们使用起来更为灵活。
例如我们可以有这样的写法:
EventHandler<string> SendNew
这在以前的版本中是不允许的。