使用泛型的好处
type safety,
所有类型相同, 减少编译报错
no casting,
无需转换, 降低性能开销
no boxing and unboxing.
可以避免装箱拆箱, 减低性能消耗
泛型约束
Constraint | Description |
---|---|
where T : <name of interface> | The type argument must be, or implement, the specified interface. |
where T : <name of base class> | The type argument must be, or derive from, the specified class |
where T : U | The type argument must be, or derive from, the supplied type argument U |
where T : new() | The type argument must have a public default constructor |
where T : struct | The type argument must be a value type |
where T : class | The type argument must be a reference type |