C#在System命名空间下提供两个委托Action和Func,这两个委托最多提供16个参数,基本上可以满足所有自定义事件所需的委托类型。几乎所有的 事件 都可以使用这两个内置的委托Action和Func进行处理。
Action委托:
Action定义提供0~16个参数,无返回值的委托
共有17种重载:
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, 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);
Func委托:
Func定义提供0~16个参数,有返回类型的委托
共有17种重载:
public delegate TResult Func<out TResult>();
public delegate TResult Func<in T, out TResult>(T arg);
public delegate TResult Func<in T1, in T2, out TResult>(T1 arg1, T2 arg2);
public delegate TResult Func<in T1, in T2, in T3, out TResult>(T1 arg1, T2 arg2, T3 arg3);
。。。。。。
public delegate TResult Func<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, out TResult>(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 TResult Func<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, out TResult>(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 TResult Func<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, out TResult>(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);
Action、Func委托与常见委托的类比
线程开始委托
public delegate void ThreadStart();
System.Threading.ThreadStart 委托 类似于 Action
事件处理委托
public delegate void EventHandler(object sender, EventArgs e);
EventHandler 委托 类似于 Action<object,EventArgs>
判定委托
public delegate bool Predicate<in T>(T obj);
Predicate<T> 委托 类似于 Func<T,bool>
比较委托
public delegate int Comparison<in T>(T x, T y);
Comparison<T> 委托 类似于 Func<T, T, int>
测试程序:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace IntegratedDelegateDemo { class Program { static void Main(string[] args) { //Action定义无返回值的委托,最多有16个参数 //Func定义一个有返回值的委托,最多有16个参数 Predicate<int> predicate = x => x > 0; Func<int, bool> func = new Func<int, bool>(predicate); Comparison<double> comparison = (x, y) => x - y > 0 ? 1 : 0; int num = -8; bool isPositive = func(num); Console.WriteLine($"【{num}】是否为正数:【{isPositive}】"); Func<double, double, int> funcComparion = new Func<double, double, int>(comparison); double xNumber = 7.666; double yNumber = 6.55; Console.WriteLine($"两个数字进行比较【{xNumber}】>【{yNumber}】的结果为【{funcComparion(xNumber, yNumber)}】"); List<string> gjqtList = new List<string>() { "百里屠苏", "风晴雪", "红玉", "方兰生" }; gjqtList.ForEach(role => Console.WriteLine(role)); Action action = () => Console.WriteLine("测试无参数,无返回值委托"); System.Threading.ThreadStart threadStart = new System.Threading.ThreadStart(action); threadStart.Invoke(); Func<short, short, int> funcComparison = (x, y) => y - x; Comparison<short> comparisonDesc = new Comparison<short>(funcComparison); short[] testSortArray = new short[] { 50, 25, 43, 69, 16, 14, 22 }; Console.WriteLine($"使用委托进行逆序排序,原数组为【{string.Join(",", testSortArray)}】"); Array.Sort(testSortArray, comparisonDesc); Console.WriteLine($"逆序排序已结束,逆序排序后的数组为【{string.Join(",", testSortArray)}】"); Console.ReadLine(); } } }