-
static string GetResult(Func<string, string> f) { string s = "ZAZA"; return f(s); } /// <summary> /// Main /// </summary> /// <param name="args"></param> static void Main(string[] args) { Console.WriteLine(GetResult(GetStr)); } /// <summary> /// 获取字符串 /// </summary> /// <param name="str"></param> /// <returns></returns> static string GetStr(string str) { return str + "is dont"; }
获取字符串转换成匿名方法
-
/// <summary> /// 获取结果 /// </summary> /// <param name="f"></param> /// <returns></returns> static string GetResult(Func<string, string> f) { string s = "ZAZA"; return f(s); } /// <summary> /// Main /// </summary> /// <param name="args"></param> static void Main(string[] args) { Console.WriteLine( GetResult( delegate (string str){ return str + "is dont"; } ) ); }
转换箭头函数
-
/// <summary> /// 获取结果 /// </summary> /// <param name="f"></param> /// <returns></returns> static string GetResult(Func<string, string> f) { string s = "ZAZA"; return f(s); } /// <summary> /// Main /// </summary> /// <param name="args"></param> static void Main(string[] args) { Console.WriteLine( GetResult( (string str)=>{ return str + "is dont"; } ) ); }
简写
-
/// <summary> /// 获取结果 /// </summary> /// <param name="f"></param> /// <returns></returns> static string GetResult(Func<string, string> f) { string s = "ZAZA"; return f(s); } /// <summary> /// Main /// </summary> /// <param name="args"></param> static void Main(string[] args) { Console.WriteLine( GetResult(str=> str + "is dont") ); }
- 参数列表 => 语句或语句块
例子:
//无返回值 无参数 //void getstring(){Console.WriteLine("xxx")} Action action = () => Console.WriteLine("xxx"); action(); //无返回值 一个参数 void getstring(string str){Console.WriteLine(str)} Action<string> actionpram =s=> Console.WriteLine(s); actionpram("SayWhat?"); //无返回值 多个参数 //void getstring(string s,string d){Console.WriteLine($"第一个参数{s},第二个参数{d}")} Action<string,string> actionprams = (s,d) => Console.WriteLine($"第一个参数{s},第二个参数{d}"); actionprams("what up","say Hi!"); //有返回值 无参数 //DateTime getDate(){return DateTime.Now;} Func<DateTime> fnc = () => DateTime.Now; //有返回值 一个参数 // DateTime getDate(string time){return Convert.ToDateTime(s);} Func<string,DateTime> fn = s =>Convert.ToDateTime(s); //有返回值 多个参数 // string getString(int a,int b){return (a+b).ToString();} Func<int,int, string> fncc = (i1,i2) => (i1+i2).ToString();