.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;
        }
    }
}
相关推荐
fengfuyao9857 分钟前
C# 高仿QQ截图工具(支持自定义快捷键)
开发语言·c#
时光追逐者15 分钟前
一款基于 .NET 开源、跨平台应用程序自动升级组件
c#·.net·.net core
啸啸说38 分钟前
1.图像的加载与保存
opencv·c#
yang_B62141 分钟前
C# ISerializable 允许对象控制自己的序列化/反序列化过程
java·开发语言·c#
海盗12341 小时前
ScottPlot在WPF的基本使用和中文乱码问题
c#·.net·wpf
胡楚昊1 小时前
BUUCTF RE 看心情写(2)
开发语言·c#
第二只羽毛1 小时前
第四章 串
大数据·数据结构·c#
ejjdhdjdjdjdjjsl1 小时前
三角形缺陷检测,划痕检测和毛刺检测
c#
我是唐青枫2 小时前
C#.NET System.Threading.Lock 深入解析:新一代 lock、底层语义与使用边界
c#·.net
格林威2 小时前
工业相机图像采集处理:从 RAW 数据到 AI 可读图像,附basler相机 C#实战代码
开发语言·人工智能·数码相机·计算机视觉·c#·视觉检测·工业相机