一,上代码
using System;namespace FuncDemo{ class Program { static void Main(string[] args) { //无参数的fanc委托 var ss1 = FuncCommon.GetT(() => { return "HelloWorld"; }); Console.WriteLine(ss1); //带参数的fanc委托 var ss = FuncCommon.GetTn ((str,m) => { var cc = str + DateTime.Now.AddDays(1).ToString(); return cc+ m; }); Console.WriteLine(ss); Console.ReadKey(); } }}
二,类代码
using System;using System.Collections.Generic;using System.Text;namespace FuncDemo{ public static class FuncCommon { public static T GetT(Func func) { T t = default(T); t = func.Invoke(); return t; } public static T GetTn (Func func) { T t = default(T); t = func.Invoke("HelloWorld","\n"+DateTime.Now.ToString()); return t; } }}
三,结果