.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;
        }
    }
}
相关推荐
xiaowu0801 小时前
C# task任务异步编程提高UI的响应性
开发语言·c#
专注VB编程开发20年10 小时前
VB.NET Asp.Net Core模板WebAPI应用-宝塔面板Linux系统通过Docker部署
linux·docker·.net
花之亡灵10 小时前
.net6 中实现邮件发送
笔记·c#·.net·代码规范
rrtt_232311 小时前
UE5 尝试接入 C# 脚本方案
ue5·c#·csharp·unrealcsharp
leslie_xin16 小时前
(原创)[开源][.Net Framework 4.5] SimpleMVVM(极简MVVM框架)更新 v1.1,增加NuGet包
c#·wpf
DXM052118 小时前
牟乃夏《ArcGIS Engine地理信息系统开发教程》学习笔记1
开发语言·经验分享·笔记·学习·arcgis·c#·arcgis engine
Wythzhfrey18 小时前
51单片机Day03---让一个LED灯闪烁
c语言·单片机·嵌入式硬件·c#·51单片机
浅陌sss18 小时前
C#容器源码分析 --- List
c#