[From msdn] Constraints on Type Parameters (C# Programming Guide)
来源:互联网 发布:淘宝商品链接 编辑:程序博客网 时间:2024/05/22 00:48
Constraints on Type Parameters (C# Programming Guide)
When you define a generic class, you can apply restrictions to the kinds of types that client code can use for type arguments when it instantiates your class. If client code attempts to instantiate your class with a type that is not allowed by a constraint, the result is a compile-time error. These restrictions are called constraints. Constraints are specified using the where contextual keyword. The following table lists the six types of constraints:
Constraint
Description
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.
- [From msdn] Constraints on Type Parameters (C# Programming Guide)
- Passing Reference-Type Parameters (C# Programming Guide)
- Constraints on Type Parameters (what does where T : class mean)
- Casting (C# Programming Guide)
- C# - Passing Reference-Type Parameters
- Grails Goodness: Type Conversion on Parameters
- Interface Properties (C# Programming Guide)
- Nullable Types (C# Programming Guide)
- Using Properties (C# Programming Guide)
- C#并行编程【from msdn】
- C#中的字符串 (C# Programming Guide)
- SharePoint Development and Programming FAQ (zz from msdn forum)
- 强调记载:Static Constructors (C# Programming Guide)
- Partial Classes and Methods (C# Programming Guide)
- Display Command Line Arguments (C# Programming Guide)
- C# 程序员参考-委托教程(From MSDN)
- Free E-books on Programming from stackoverflow
- Differences Between C++ Templates and C# Generics (C# Programming Guide)
- C#异步调用与多线程
- 注册表
- SQL Server 索引结构及其使用[转]
- 0701
- VC6.0打开文件 内存错误
- [From msdn] Constraints on Type Parameters (C# Programming Guide)
- C#开发ActiveX(创建,发布,注册)
- 如何申请Android Map API KEY 和 如何使用
- Java中使用BigDecimal进行浮点数精确计算、超大整数、浮点数等计算
- Special documentation blocks for doxygen
- 关于字符串操作strncat
- DedeCMS V5.6功能列表
- 生产订单归类总结
- 关于。。。必须放在runat=server的窗体标记内小结(asp.net)