C# 中的泛型与重载

来源:互联网 发布:java 开发 编辑:程序博客网 时间:2024/06/15 01:46

C#中如泛型与重载有相同类型(类型等价),普通函数会优先调用,而且在决议过程中,泛型函数中的where条件未计入考虑。测试程序如下:

class A{        public override string ToString()        {            return "Class A";        }    }   interface ITest    {        void test();    }    class B:ITest    {        public void test()        {            Console.WriteLine("Hello,World.");        }    }class Program    {        static void Test<T,U>(T t,U u)            where T:ITest            where U:class        {            t.test();            Console.WriteLine("where");        }        static void Test<T>(T t, string name)        {            Console.WriteLine("name:{0}",name);        }        static void Main(string[] args)        {            B b = new B();            A a = new A();            Test(b, "test");//调用Test<T>(T t, string name)            Test(b, a);//调用Test<T,U>(T t,U u)            Console.ReadLine();        }    }
测试结果如下:

name:testHello,World.where


0 0