.NET Framework中自带的泛型委托Func

Func<>是.NET Framework中自带的泛型委托,可以接收一个或多个输入参数,并且有返回值,和Action类似,.NET基类库也提供了多达16个输入参数的Func委托,输出参数只有1个。

1、Func泛型委托

.NET Framework为我们提供了多达16个参数的Func委托定义,对于常见的开发场景已经完全够用。

如下图,

注意: TResult是返回值的类型。

示例说明:

Func<>:委托至少0个参数,至多16个参数,有返回值。

Func<string>:表示无参,有返回值的委托。

Func<int,string> :表示有传入参数int,string类型返回值的委托。

Func<int,string,bool>:表示有传入参数int、string,bool类型返回值的委托。

FuncAction<int,int,int,int>:表示有传入3个int型参数,int类型返回值的委托。

2、Func泛型委托的使用

Func泛型委托的使用方法,可以通过下面代码看一下,

例如,

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FuncDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // 无参数无返回值的委托
            Func<string> func1 = new Func<string>(FuncWithNoParaReturn);
            Console.WriteLine("返回值={0}",func1());
            Console.WriteLine("----------------------------");
            // 使用delegate
            Func<string> func2 = delegate { Console.WriteLine("使用delegate");             return "error"; };
            // 执行
            Console.WriteLine("返回值={0}",func2());
            Console.WriteLine("----------------------------");
            // 使用匿名委托
            Func<string> func3 = () => { Console.WriteLine("匿名委托");            return "func"; };
            Console.WriteLine("返回值={0}",func3());
            Console.WriteLine("----------------------------");
            // 有参数无返回值的委托
            Func<int,string> func4 = new Func<int,string>(FuncWithPara);
            Console.WriteLine("返回值={0}",func4(11));
            Console.WriteLine("----------------------------");
            // 使用delegate
            Func<int,string> func5 = delegate (int i) { Console.WriteLine($"使用delegate的委托,参数值是:{i}");             return "delegate (int i)";};
            Console.WriteLine("返回值={0}",func5(22));
            Console.WriteLine("----------------------------");
            // 使用匿名委托
            Func<string,string> func6 = (string s) => { Console.WriteLine($"使用匿名委托,参数值是:{s}");            return "func6"; };
            func6("C#");
            Console.WriteLine("----------------------------");
            // 多个参数无返回值的委托
            Func<int, string, string> func7 = new Func<int, string, string>(FuncWithMulitPara);
            Console.WriteLine("返回值={0}",func7(33, "Java"));
            Console.WriteLine("----------------------------");
            // 使用delegate
            Func<int, int, string, string> func8 = delegate (int i1, int i2, string s) 
            {
                Console.WriteLine($"三个参数的Func委托,参数1的值是:{i1},参数2的值是:{i2},参数3的值是:{s}");
                return s;
            };
            Console.WriteLine("返回值={0}",func8(44, 55, "Python"));
            Console.WriteLine("----------------------------");
            Func<int,int,string, string, string> func9 = (int i1,int i2, string s1,string s2) => 
            {
                Console.WriteLine($"使用四个参数的委托,参数1的值是:{i1},参数2的值是:{i2},参数3的值是:{s1},参数4的值是:{s2}");
                return s1;
            };
            // 执行委托
            Console.WriteLine("返回值={0}",func9(66,77, "C","CJavaPy"));
            Console.ReadKey();
        }




        static string FuncWithNoParaReturn()
        {
            Console.WriteLine("无参数有返回值的Func委托");
            return "value";
        }

        static string FuncWithPara(int i)
        {
            Console.WriteLine($"有参数有返回值的委托,参数值是:{i}");
            return "FuncWithPara";
        }

        static string FuncWithMulitPara(int i,string s)
        {
            Console.WriteLine($"有两个参数无返回值的委托,参数1的值是:{i},参数2的值是:{s}");
            return s;
        }
    }
}
相关推荐
唐青枫3 小时前
C#.NET 定时任务与队列利器:Hangfire 完整教程
c#·.net
hez20109 小时前
Runtime Async - 步入高性能异步时代
c#·.net·.net core·clr
追逐时光者21 小时前
.NET Fiddle:一个方便易用的在线.NET代码编辑工具
后端·.net
mudtools1 天前
.NET驾驭Word之力:玩转文本与格式
c#·.net
唐青枫1 天前
C#.NET 数据库开发提速秘籍:SqlSugar 实战详解
c#·.net
追逐时光者1 天前
精选 4 款基于 .NET 开源、功能强大的 Windows 系统优化工具
后端·.net
mudtools2 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
大飞pkz2 天前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
唐青枫2 天前
从入门到进阶:C#.NET Stopwatch 计时与性能测量全攻略
c#·.net
私人珍藏库2 天前
[Windows] 微软 .Net 运行库离线安装包 | Microsoft .Net Packages AIO_v09.09.25
microsoft·.net·运行库