C#中的Func泛型类型
Func泛型类型存储匿名方法,并且是参数化类型。
在下面的示例中,我们有4个func类型实例-
第一种类型接收int并返回字符串
Func<int, string> one = (p) => string.Format("{0}", p);第二种类型接收bool和long并返回字符串
Func<bool, long, string> two = (q, p) =>string.Format("{0} and {1}", q, p);第三种类型接收bool和int并返回字符串
Func<bool, int, string> three = (q, p) => string.Format("{0} and {1}", q, p);第四种类型接收十进制并返回字符串
Func<decimal, string> four = (p) =>string.Format("{0}", p);让我们看看如何显示它们-
示例
using System;
using System.IO;
namespace Demo {
class Program {
static void Main(string[] args) {
//四个func类型实例
//第一种类型接收int并返回字符串
Func<int, string> one = (p) =>
string.Format("{0}", p);
// second type receives bool & long and returns string
Func<bool, long, string> two = (q, p) =>
string.Format("{0} and {1}", q, p);
// three type receives bool & int and returns string
Func<bool, int, string> three = (q, p) =>
string.Format("{0} and {1}", q, p);
//第四个类型接收十进制并返回字符串
Func<decimal, string> four = (p) =>
string.Format("{0}", p);
Console.WriteLine(one.Invoke(25));
Console.WriteLine(two.Invoke(false, 76756566));
Console.WriteLine(three.Invoke(true, 50));
Console.WriteLine(four.Invoke(1.2m));
}
}
}输出结果
25 False and 76756566 True and 50 1.2
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短