Constraints on generic parameters
When defining generic classes, you canCodeYou can apply restrictions on the type types used for type parameters during class instantiation. If the client code attempts to instantiate a class using a type not allowed by a certain constraint, a compile-time error will occur. These restrictions are called constraints. The constraint is to useWhereSpecified by the context keyword. The following table lists the six types of constraints:
| Constraints |
Description |
T: Structure |
The type parameter must be a value type. You can specify the Division { Track ('ctl00 _ libframe_ctl06 | ctl00_libframe_ctl07 ', this ); } "Href =" http://msdn2.microsoft.com/zh-cn/library/system.nullable (vs.80). aspx ">NullableAny value type other. For more information, see { Track ('ctl00 _ libframe_ctl06 | ctl00_libframe_ctl08 ', this ); } "Href =" http://msdn2.microsoft.com/zh-cn/library/2cf62fcy (vs.80). aspx ">Use a null type (C # programming guide). |
T: Class |
The type parameter must be a reference type, including any class, interface, delegate, or array type. |
T: New () |
The type parameter must have a public constructor without parameters. When used together with other constraints,New ()The constraint must be specified at the end. |
T: <Base Class Name> |
The type parameter must be a specified base class or derived from the specified base class. |
T: <Interface Name> |
The type parameter must be a specified interface or implement the specified interface. Multiple interface constraints can be specified. The constraint interface can also be generic. |
T: u |
The type parameter provided for T must be provided for U or derived from U. This is called the bare type constraint. |
{
Showhidecollapsiblearea ('ctl00 _ libframe_ctl17609f64e ', 'ctl00 _ libframe_ctl17img'); Return false;
} "Href =" http://msdn2.microsoft.com/zh-cn/library/d5x73970 (vs.80). aspx # ">Bare type constraints
The generic type parameters used as constraints are called Bare type constraints. When a member function with its own type parameters needs to constrain this parameter to include type parameters, the bare type constraints are useful, as shown in the following example:
Class List < T >
{
Code_collapse_toggle (this );
} "Src =" http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif "alt =" "align =" TOP "> {
Code_collapse_toggle (this );
} "Src =" http://writeblog.csdn.net/Images/OutliningIndicators/ContractedBlock.gif "alt =" "align =" TOP "> {
{
Code_collapse_toggle (this );
} "Src =" http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif "alt =" "align =" TOP "> {
Code_collapse_toggle (this );
} "Src =" http://writeblog.csdn.net/Images/OutliningIndicators/ContractedSubBlock.gif "alt =" "align =" TOP "> Void Add < U > (List < U > Items) where u: T {/**//**/}
}
In the preceding example,TInAddThe method context is a bare type constraint, whileListClass is an unbound type parameter in the context.
Bare type constraints can also be used in generic class definitions. Note that the bare type constraint must be declared in angle brackets with any other type parameter:
// Naked type ConstraintPublic ClassSampleclass <t, u, v> where T: V {}
The bare type constraints of generic classes have very limited functions, because the compiler assumes that a bare type constraint is derived fromSystem. ObjectNo other assumptions. When you want to force the inheritance relationship between two type parameters, you can use the bare type constraint for the generic class.
{
Track ('ctl00 _ libframe_ctl09 | ctl00_libframe_ctl10 ', this );
} "Href =" http://msdn2.microsoft.com/zh-cn/library/0x6a29h6 (vs.80). aspx ">Generic introduction (C # programming guide).