C#泛型接口

使用泛型可以定义接口,在接口中定义的方法可以带泛型参数。----------《C#高级编程(第7版)》清华大学出版社

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            TestClass testClass = new TestClass();
            testClass.CWType(123);
            TestClass2 testClass2 = new TestClass2();
            testClass2.CWType("123");
            Console.ReadKey();
        }
    }

    public interface ITestInter<in T>
    {
        void CWType(T t);
    }
    public class TestClass : ITestInter<int>
    {
        public void CWType(int num)
        {
            Console.WriteLine(num.GetType());
        }
    }

    public class TestClass2 : ITestInter<String>
    {
        public void CWType(String str)
        {
            Console.WriteLine(str.GetType());
        }
    }

}
posted @ 2020-01-13 11:06  小包2017  阅读(314)  评论(0编辑  收藏  举报