C#统一委托Func与Action

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>

测试程序:

cs 复制代码
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();
        }
    }
}

运行截图:

相关推荐
IT技术分享社区14 分钟前
C#实战:使用腾讯云识别服务轻松提取火车票信息
开发语言·c#·云计算·腾讯云·共识算法
△曉風殘月〆7 小时前
WPF MVVM入门系列教程(二、依赖属性)
c#·wpf·mvvm
逐·風9 小时前
unity关于自定义渲染、内存管理、性能调优、复杂物理模拟、并行计算以及插件开发
前端·unity·c#
m0_6569747412 小时前
C#中的集合类及其使用
开发语言·c#
九鼎科技-Leo12 小时前
了解 .NET 运行时与 .NET 框架:基础概念与相互关系
windows·c#·.net
九鼎科技-Leo15 小时前
什么是 ASP.NET Core?与 ASP.NET MVC 有什么区别?
windows·后端·c#·asp.net·mvc·.net
.net开发15 小时前
WPF怎么通过RestSharp向后端发请求
前端·c#·.net·wpf
小乖兽技术15 小时前
C#与C++交互开发系列(二十):跨进程通信之共享内存(Shared Memory)
c++·c#·交互·ipc
幼儿园园霸柒柒15 小时前
第七章: 7.3求一个3*3的整型矩阵对角线元素之和
c语言·c++·算法·矩阵·c#·1024程序员节
平凡シンプル18 小时前
C# EF 使用
c#