为什么在空检查后将 int 设置为 0 被认为没有实际意义?

Why is setting an int to 0 considered moot after a null check?

我有这段代码,如果它为 null,则将 int val 设置为 0:

if (null == frbdcbl.QtyOrdered)
{
    frbdcbl.QtyOrdered = 0;
}

...但是分配是灰色的,好像这样的操作没有实际意义。为什么会这样?毕竟,null != 0.

正在检查的值没有默认值 0 或类似的值:

public class FillRateByDCByLocation
{
    . . .
    public int QtyOrdered { get; set; }
}

灰色赋值的代码(AddFillRateByDCByLocationRow(()) 调用如下:

foreach (FillRateByDCByLocation frbdcbl in _fillRateByDCByLocGroupedSortedSummed)
{
    AddFillRateByDCByLocationRow(frbdcbl);
}

它是一个可为空的整数吗?如果不是,它会变灰,因为 int 永远不会为空。

QtyOrdered 不能是 null 因为 null 不是 int 的有效值,对于任何 struct.

都是一样的

值类型(int、long、enum 等)不能为 null。永远不会完成分配,因此显示为灰色。