如果将 [null] 值发送到 int 数组会发生什么?

问题描述 投票:0回答:1

我会问一个简单的问题

public int[] Ids { get; set; } 

如果我将

[null]
发送到我的参数,会出现什么错误?

public int?[] Ids { get; set; } 

这样改的话会报错吗? (还是发送

[null]

c# arrays null
1个回答
0
投票

如果我将 [null] 发送到我的参数,我会得到什么错误?

如果类型是

int[]
你会得到

CS0037:无法将 null 转换为“int”,因为它是不可为 null 的值类型

因为

null
不能分配给
int

但是,如果将类型更改为

int?[]
,这将是有效的赋值,因为
int?
是可以为 null 的类型并且可以保存 null 值。

© www.soinside.com 2019 - 2024. All rights reserved.