Fork me on GitHub

C# 泛型里使用四则运算的办法,委托的妙用

直接上代码

 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();
        }

结果:

 

posted @ 2023-01-26 19:53  HelloLLLLL  阅读(73)  评论(0编辑  收藏  举报