我会问一个简单的问题
public int[] Ids { get; set; }
如果我将
[null]
发送到我的参数,会出现什么错误?
public int?[] Ids { get; set; }
这样改的话会报错吗? (还是发送
[null]
)
如果我将 [null] 发送到我的参数,我会得到什么错误?
如果类型是
int[]
你会得到
CS0037:无法将 null 转换为“int”,因为它是不可为 null 的值类型
因为
null
不能分配给int
。
但是,如果将类型更改为
int?[]
,这将是有效的赋值,因为 int?
是可以为 null 的类型并且可以保存 null 值。