c# - 为什么我不能将通用列表声明为可为空?

标签 c# null nullable

我正在尝试使用以下代码:

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/

相关文章:

java - 我正在尝试从 Android 设备获取光传感器数据以在 Unity 应用程序中使用

c# - XML LINQ 查询不返回任何内容

c# - 如果值为 NULL,为什么 nullable int (int?) 不会通过 "+="增加值?

c# - 为什么在 Excel 中比较 1<' 或将任何数字与带有 < 的任何文本进行比较结果为真?

c# - 如何确定非空对象是否为可空结构?

c# - WPF SelectedItem 颜色在列表失去焦点时消失

c# - Visual Studio 显示了很多错误,但只有一个实际错误

PHP/MySQL 插入空值

objective-c - [__NSArrayM 插入对象 :atIndex:]: object cannot be nil - how determine where is the error?

c# - 如何检查文本中是否包含 "not allowed"单词或符号?