C# 泛型类型约束 where
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                C# 泛型类型约束 where
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                 1  class Program {
 2         static void Main(string[] args) {
 3
 4         }
 5     }
 6
 7     interface IMyInterface {
 8         void Method1();
 9     }
10
11     //一、六種類型約束
12     //1、類型參數(shù)必須是引用類型
13     class MyClass1<T> where T : class {}
14     //2、類型參數(shù)必須是值類型
15     class MyClass2<T> where T : struct {}
16     //3、類型參數(shù)必須具有無參公共構(gòu)造函數(shù)
17     class MyClass3<T> where T : new() {}
18     //4、類型參數(shù)必須是指定的類型或及其子類
19     class MyClass4<T> where T : Program { }
20     //5、類型參數(shù)必須是實(shí)現(xiàn)了指定接口的對象
21     class MyClass5<T> where T : IMyInterface { }
22     //6、U類型參數(shù)必須為T類型或及其子類
23     class List<T>
24     {
25         void Method<U>(List<U> items) where U : T
26         {
27             //TODO
28             //Do something...
29         }
30     }
31
32     //二、約束可以用于類、方法和委托
33     delegate void MyDelegate<T>() where T:class;
總結(jié)
以上是生活随笔為你收集整理的C# 泛型类型约束 where的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Task VS ValueTask
- 下一篇: Date对象
