.NET支持的類型參數約束有以下五種:
where T : struct | T必須是一個結構類型
where T : class | T必須是一個Class類型
where T : new() | T必須要有一個無參構造函數
where T : NameOfBaseClass | T必須繼承名為NameOfBaseClass的類
where T : NameOfInterface | T必須實現名為NameOfInterface的接口
泛型的Where
泛型的Where能夠對類型參數作出限定。有以下幾種方式。
·where T : struct 限制類型參數T必須繼承自System.ValueType。
·where T : class 限制類型參數T必須是引用類型,也就是不能繼承自System.ValueType。
·where T : new() 限制類型參數T必須有一個缺省的構造函數
·where T : NameOfClass 限制類型參數T必須繼承自某個類或實現某個接口。
以上這些限定可以組合使用,比如: public class Point where T : class, IComparable, new()