C#泛型约束

1. where T : struct    对于结构约束,类型T必须是值类型

2. where T : class     对于类约束,类型T必须是引用类型

3. where T : 接口名称IFoo  对于指定接口IFoo约束,类型T必须实现指定接口IFoo

4. where T : 类名Foo     对于指定类Foo约束,类型T必须是Foo对象或者是继承自Foo得对象

5. where T : new()     构造函数约束,类型T必须有一个无参构造函数

6. where T1:T2       裸类型约束,类型T1派生自泛型类型T2

where T : struct 对于结构约束,类型T必须是值类型
where T : class 对于类约束,类型T必须是引用类型
where T : 接口名称IFoo 对于指定接口IFoo约束,类型T必须实现指定接口IFoo
where T : 类名Foo 对于指定类Foo约束,类型T必须是Foo对象或者是继承自Foo得对象
where T : new() 构造函数约束,类型T必须有一个无参构造函数
 where T1:T2  裸类型约束,类型T1派生自泛型类型T2

1.

    static void Main(string[] args)
        {
            TestT(123);
            TestT('a');
            //TestT("123");  报错
            Console.ReadKey();
        }
        public static void TestT<T>(T t) where T : struct
        {
            Console.WriteLine(t.GetType());
        }    

2.

  static void Main(string[] args)
        {
            TestT("123");
            //TestT(123);//报错
            Console.ReadKey();
        }
        public static void TestT<T>(T t) where T : class
        {
            Console.WriteLine(t.GetType());
        }

3.

 class Program
    {
        static void Main(string[] args)
        {
            TestClass testClass = new TestClass();
            TestT(testClass);
            Object obj = new Object();
            //TestT(obj);//报错
            Console.ReadKey();
        }
        public static void TestT<T>(T t) where T : TestInterface
        {
            Console.WriteLine(t.GetType());
        }
    }
    interface TestInterface
    { }

    class TestClass : TestInterface
    {
    }

4.

class Program
    {
        static void Main(string[] args)
        {
            Father father = new Father();
            TestT(father);
            Childern childern = new Childern();
            TestT(childern);
            Object obj = new Object();
            //TestT(obj);//报错
            Console.ReadKey();
        }
        public static void TestT<T>(T t) where T : Father
        {
            Console.WriteLine(t.GetType());
        }
    }

    class Father
    {
    }
    class Childern:Father
    {
    }

5.

class Program
    {
        static void Main(string[] args)
        {
            TestClass testClass = new TestClass();
            TestT(testClass);
            TestClass2 testClass2 = new TestClass2(123);
            //TestT(testClass2);//报错
            Console.ReadKey();
        }
        public static void TestT<T>(T t) where T : new()
        {
            Console.WriteLine(t.GetType());
        }
    }

    class TestClass
    {
    }
    class TestClass2
    {
        public TestClass2(int num)
        {
        }
    }

6.

class Program
    {
        static void Main(string[] args)
        {
            TestClass testClass = new TestClass();
            TestClass2 testClass2 = new TestClass2();
            TestT(testClass2,testClass);
            Console.ReadKey();
        }
        public static void TestT<T1,T2>(T1 t,T2 t2) where T1 : T2
        {
            Console.WriteLine(t.GetType());
        }
    }

    class TestClass
    {
    }
    class TestClass2  : TestClass
    {
        public TestClass2()
        {

        }
    }
原文地址:https://www.cnblogs.com/xiaobao2017/p/12186213.html