码迷,mamicode.com
首页 > 其他好文 > 详细

where T : class

时间:2020-07-01 09:48:21      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:where   col   actions   exce   control   contain   traints   eui   you   

122
 

where T : struct

The type argument must be a value type. Any value type except Nullable can be specified. See Using Nullable Types (C# Programming Guide) for more information.

where T : class

The type argument must be a reference type, including any class, interface, delegate, or array type. (See note below.)

where T : new() The type argument must have a public parameterless constructor. When used in conjunction with other constraints, the new() constraint must be specified last.

where T : [base class name]

The type argument must be or derive from the specified base class.

where T : [interface name]

The type argument must be or implement the specified interface. Multiple interface constraints can be specified. The constraining interface can also be generic.

where T : U

The type argument supplied for T must be or derive from the argument supplied for U. This is called a naked type constraint.

 

 1  struct MyStruct { } // structs are value types
 2 
 3     class MyClass1 { } // no constructors defined, so the class implicitly has a parameterless one
 4 
 5     class MyClass2 // parameterless constructor explicitly defined
 6     {
 7         public MyClass2() { }
 8     }
 9 
10     class MyClass3 // only non-parameterless constructor defined
11     {
12         public MyClass3(object parameter) { }
13     }
14 
15     class MyClass4 // both parameterless & non-parameterless constructors defined
16     {
17         public MyClass4() { }
18         public MyClass4(object parameter) { }
19     }
20 
21     interface INewable<T>
22         where T : new()
23     {
24     }
25 
26     interface INewableReference<T>
27         where T : class, new()
28     {
29     }
30 
31     class Checks
32     {
33         INewable<int> cn1; // 允许: int是包含无参数构造函数的结构体
34         INewable<string> n2; // 不允许: string是带有参数构造函数的类
35         INewable<MyStruct> n3; // 允许: MyStruct是包含无参数构造函数的结构体
36         INewable<MyClass1> n4; // 允许: MyClass1是包含无参数构造函数的类
37         INewable<MyClass2> n5; // 允许: MyClass2是包含无参数构造函数的类
38         INewable<MyClass3> n6; // 不允许: MyClass3是包含有参数构造函数的类
39         INewable<MyClass4> n7; // 允许: MyClass4是包含无参数构造函数的类
40 
41         INewableReference<int> nr1; // 不允许: int不是引用类型(而是结构体)
42         INewableReference<string> nr2; // 不允许: string是带有参数构造函数的类
43         INewableReference<MyStruct> nr3; // 不允许: MyStruct不是引用类型(而是结构体)
44         INewableReference<MyClass1> nr4; // 允许: MyClass1是包含无参数构造函数的类
45         INewableReference<MyClass2> nr5; // 允许: MyClass2是包含无参数构造函数的类
46         INewableReference<MyClass3> nr6; // 不允许: MyClass3是包含有参数构造函数的类
47         INewableReference<MyClass4> nr7; // 允许: MyClass4是包含无参数构造函数的类
48     }

这个是翻译别人的加上自己的理解!!

where T : class

标签:where   col   actions   exce   control   contain   traints   eui   you   

原文地址:https://www.cnblogs.com/Grasslands/p/13217244.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!