.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;
        }
    }
}
相关推荐
ahxdyz2 小时前
.NET平台MCP
ai·.net·mcp
の天命喵星人3 小时前
.net 使用NLog记录日志
.net
绿荫阿广4 小时前
将SignalR移植到Esp32—让小智设备无缝连接.NET功能拓展MCP服务
.net·asp.net core·mcp
人工智能AI技术4 小时前
【C#程序员入门AI】本地大模型落地:用Ollama+C#在本地运行Llama 3/Phi-3,无需云端
人工智能·c#
MyBFuture8 小时前
C#数组详解:一维二维与交错数组
开发语言·windows·c#·visual studio·vision pro
有来技术9 小时前
ASP.NET Core 权限管理系统(RBAC)设计与实现|vue3-element-admin .NET 后端
vue.js·后端·c#·asp.net·.net
云草桑10 小时前
15分钟快速了解 Odoo
数据库·python·docker·postgresql·.net·odoo
张人玉11 小时前
C#WinFrom中show和ShowDialog的区别
开发语言·microsoft·c#
m0_7482331711 小时前
C#:微软的现代编程利器
开发语言·microsoft·c#