Action 和 Func 的用法以及區別
阿新 • • 發佈:2017-11-13
delegate class div clas 返回 span pre console 技術分享
Action 無返回值
Func 有返回值,且最後一個參數為返回值
Action用法
public static void test(string s) { Console.WriteLine("name1:{0}", s); } static void Main(string[] args) { Action<string> action_ = new Action<string>(test); action_ -= test; action_+= delegate(string s) { Console.WriteLine("name2:{0}", s); }; action_("1"); }
測試結果
Func 和Action 用法類似,只是其有返回值
說明:Action 和 Func 委托方法,就是一種方法的泛型,可以將方法名作為參數進行傳遞。
Action 和 Func 的用法以及區別