簡體   English   中英

將通用類型限制定義為可為空的原始類型

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM