直接上代碼
public static class TestGenricCalc { public static T Clac<T>(T t1, T t2,Func<T,T,T>func) where T : struct { return func(t1, t2); } }
static void Main(string[] args) { Console.WriteLine("Hello World!"); var c=TestGenricCalc.Clac<float>(1, 2, (a, b) => { return a + b; }); Console.WriteLine(c); Console.ReadKey(); }
結果: