C#泛型

来源:互联网 发布:气象数据知识试题 编辑:程序博客网 时间:2024/06/08 06:55

使用泛型的好处


type safety, 

所有类型相同, 减少编译报错


no casting, 

无需转换, 降低性能开销


no boxing and unboxing.

可以避免装箱拆箱, 减低性能消耗


泛型约束

ConstraintDescriptionwhere 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 classwhere T : UThe type argument must be, or derive from, the supplied type argument Uwhere T : new()The type argument must have a public default constructorwhere T : structThe type argument must be a value typewhere T : classThe type argument must be a reference type