.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;
        }
    }
}
相关推荐
假装我不帅6 小时前
wsl+vscode开发.net项目
ide·vscode·.net
偶尔的鼠标人8 小时前
Avalonia DataGrid 控件的LostFocus事件会多次触发
开发语言·c#
ytttr8738 小时前
C# 仿QQ聊天功能实现 (SQL Server数据库)
数据库·oracle·c#
future_studio10 小时前
聊聊 Unity(小白专享、C# 小程序 之 图片播放器)
unity·小程序·c#
c#上位机16 小时前
wpf中Grid的MouseDown 事件无法触发的原因
c#·wpf
CodeCraft Studio18 小时前
国产化PDF处理控件Spire.PDF教程:如何在 C# 中从 HTML 和 PDF 模板生成 PDF
pdf·c#·html·.net·spire.pdf·pdf文档开发·html创建模板pdf
ysdysyn19 小时前
.NET 10深度解析:性能革新与开发生态的全新篇章
c#·.net
L X..1 天前
Unity 光照贴图异常修复笔记
unity·c#·游戏引擎
reasonsummer1 天前
【办公类-115-06】20250920职称资料上传04——docx复制、docx转PDF(课程表11个)
开发语言·windows·python·c#
fs哆哆1 天前
在VB.NET中,有没有 ?.这个运算符
java·开发语言·.net