public delegate TResult Func<in T, out TResult>(T arg);
参数类型
T:此委托方法的参数类型
TResult:此委托方法的返回值类型
arg:此委托封装方法的参数
using System; namespace 编码练习 { //将名字大小写转换 class Program { public static string UppercaseString(string name) { return name.ToUpper(); } public static void Main() { Func<string, string> translation = UppercaseString; string name = "make"; Console.WriteLine(translation(name)); } } }
using System; namespace 编码练习 { //将名字大小写转换 class Program { //使用匿名方法 public static void Main() { Func<string, string> translation = delegate (string s) { return s.ToUpper(); }; string name = "make"; Console.WriteLine(translation(name)); } } }
using System; namespace 编码练习 { //将名字大小写转换 class Program { //使用lambda表达式 public static void Main() { Func<string, string> translation = x => x.ToUpper(); string name = "make"; Console.WriteLine(translation(name)); } } }