[英]Define generic type limit to nullable primitive types
我可以使用可為空的原始數據類型定義泛型類型嗎? 就像是
public class DataTypeHolder<T> :List<T> where T : struct
{
public DataTypeHolder()
{
}
public void DoubleValueWithNull()
{
var count = this.Count;
for (int from = 0; from < count; from++)
{
this.Add(null); //// **this is needed..but causing compilation error**
}
}
}
以后應該可以
DataTypeHolder<double?> d = new DataTypeHolder<double?>();
您必須像這樣定義您的課程:
public class DataTypeHolder<T> :List<T?> where T : struct
與以下內容相同:
public class DataTypeHolder<T> :List<Nullable<T>> where T : struct
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.