我正在尝试使用以下代码:
private Nullable<List<IpAddressRange>> ipAddressRangeToBind;
但我收到以下警告:
The type List must be a non-nullable value type in order to use it as a parameter 'T' in the generic type or method 'System.Nullable'.
最佳答案
List<T>
已经是引用类型(对于任何类型的 T
) - 您只能声明 Nullable<T>
其中 T
是不可为 null 的值类型(声明为 Nullable<T> where T : struct
)。
但这没关系,因为如果你只是声明:
private List<IpAddressRange> ipAddressRangeToBind;
那你还可以拥有
ipAddressRangeToBind = null;
因为引用类型总是可为空。
关于c# - 为什么我不能将通用列表声明为可为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7418786/