网站建设作业做一个简单的网站,成品网站 售卖,找网页模板的网站,WordPress文章固定连接htmlFunc是.NET Framework中自带的泛型委托#xff0c;可以接收一个或多个输入参数#xff0c;并且有返回值#xff0c;和Action类似#xff0c;.NET基类库也提供了多达16个输入参数的Func委托#xff0c;输出参数只有1个。
1、Func泛型委托
.NET Framework为我们提…Func是.NET Framework中自带的泛型委托可以接收一个或多个输入参数并且有返回值和Action类似.NET基类库也提供了多达16个输入参数的Func委托输出参数只有1个。
1、Func泛型委托
.NET Framework为我们提供了多达16个参数的Func委托定义对于常见的开发场景已经完全够用。
如下图 注意TResult是返回值的类型。
示例说明
Func委托至少0个参数至多16个参数有返回值。
Funcstring表示无参有返回值的委托。
Funcint,string 表示有传入参数intstring类型返回值的委托。
Funcint,string,bool表示有传入参数int、stringbool类型返回值的委托。
FuncActionint,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){// 无参数无返回值的委托Funcstring func1 new Funcstring(FuncWithNoParaReturn);Console.WriteLine(返回值{0},func1());Console.WriteLine(----------------------------);// 使用delegateFuncstring func2 delegate { Console.WriteLine(使用delegate); return error; };// 执行Console.WriteLine(返回值{0},func2());Console.WriteLine(----------------------------);// 使用匿名委托Funcstring func3 () { Console.WriteLine(匿名委托); return func; };Console.WriteLine(返回值{0},func3());Console.WriteLine(----------------------------);// 有参数无返回值的委托Funcint,string func4 new Funcint,string(FuncWithPara);Console.WriteLine(返回值{0},func4(11));Console.WriteLine(----------------------------);// 使用delegateFuncint,string func5 delegate (int i) { Console.WriteLine($使用delegate的委托参数值是{i}); return delegate (int i);};Console.WriteLine(返回值{0},func5(22));Console.WriteLine(----------------------------);// 使用匿名委托Funcstring,string func6 (string s) { Console.WriteLine($使用匿名委托参数值是:{s}); return func6; };func6(C#);Console.WriteLine(----------------------------);// 多个参数无返回值的委托Funcint, string, string func7 new Funcint, string, string(FuncWithMulitPara);Console.WriteLine(返回值{0},func7(33, Java));Console.WriteLine(----------------------------);// 使用delegateFuncint, 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(----------------------------);Funcint,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;}}
}