添加两个可为空的整数不起作用
Adding two nullable integers not working
我有一个很奇怪的问题。问题看起来很有趣和简单,但它让我很生气。
我在 class 中有一个可为空的整数,声明为
public int? count { get; set; }
我有这个 class 的对象数组 (additionalCell) 和另一个 class 的对象,叫做 currentPixelTotalCell。我想添加数组中所有对象的计数变量的值并将其存储在 currentPixelTotalCell 的计数变量中。
我的代码如下。但是在调试时,我看到左侧部分只有在退出循环后才具有 null 值,尽管所有对象中的 count 变量都具有非空值。
for(int i = 0; i < 5; i++)
{
currentPixelTotalCell.count += additionalCell[i].count;
}
知道为什么会这样吗?有没有不同的方法来添加它们?我一无所知。
编辑:
忘记提到这个了。当我有断点并检查第一次迭代本身时,它不会加起来。
例如。如果 additionalCell[0].count 是 10。那么 currentPixelTotalCell.count 的值曾经只有在第一次迭代中执行了内线之后才为 null。
如果结果是 null
那么
- 要么
currentPixelTotalCell.count
是 null
- 一些additionalCell[i].count是
null
确保两个 null
都在控制之下
// currentPixelTotalCell.count is not null
currentPixelTotalCell.count = 0;
for(int i = 0; i < 5; i++)
{
// if additionalCell[i].count is null treat it as 0
currentPixelTotalCell.count += additionalCell[i].count ?? 0;
}
您可以尝试使用 Linq 作为替代方法:
currentPixelTotalCell.count = additionalCell
.Take(5) // if you want take just first 5 items
.Sum(item => item ?? 0);
将您的内部循环更改为:
currentPixelTotalCell.count += (additionalCell[i].count ?? 0);
避免在右手值之一为空的情况下将总数设置为空。
我猜结果是 null
因为其中一个值是 null
.
怎么样:
currentPixelTotalCell.count += additionalCell.Select(x => x.count)
.Where(x => x.HasValue)
.Sum();
或
currentPixelTotalCell.count += additionalCell.Sum(x => x.count ?? 0);
不要忘记在某处初始化 currentPixelTotalCell.count
或用简单的赋值 =
替换 +=
。
难道是需要先将currentPixelTotalCell.count
变量初始化为0?
currentPixelTotalCell.count = 0;
for(int i = 0; i < 5; i++)
{
currentPixelTotalCell.count += additionalCell[i].count;
}
或者您可能必须检查 AdditionalCell 对象中的空值?
for(int i = 0; i < 5; i++)
{
currentPixelTotalCell.count += (additionalCell[i].count ?? 0)
}
有一个名为 .GetValueOrDefault()
的方法,它将为您提供 Nullable<T>
的默认值。如果值为 null
:
,您可以使用它来分配 0
for(int i = 0; i < 5; i++)
{
currentPixelTotalCell.CmvF =currentPixelTotalCell.CmvF.GetValueOrDefault() + additionalCell[i].CmvF.GetValueOrDefault();
}
访问前需要将currentPixelTotalCell.count初始化为0
记住“a += b”只是“a = a + b”的语法糖。
因为 a 为 null,您实际上是在执行“a = null + b”并且 null 加上某项等于 null。
同样因为同样的约束,需要保证右边的值也不为null。在您的情况下,更简单的方法是简单地使用 GetValueOrDefault 方法。
综上所述,您的最终解决方案应该类似于:
currentPixelTotalCell.count = 0;
for(int i = 0; i < 5; i++)
{
currentPixelTotalCell.count += additionalCell[i].count.GetValueOrDefault();
}
我有一个很奇怪的问题。问题看起来很有趣和简单,但它让我很生气。
我在 class 中有一个可为空的整数,声明为
public int? count { get; set; }
我有这个 class 的对象数组 (additionalCell) 和另一个 class 的对象,叫做 currentPixelTotalCell。我想添加数组中所有对象的计数变量的值并将其存储在 currentPixelTotalCell 的计数变量中。
我的代码如下。但是在调试时,我看到左侧部分只有在退出循环后才具有 null 值,尽管所有对象中的 count 变量都具有非空值。
for(int i = 0; i < 5; i++)
{
currentPixelTotalCell.count += additionalCell[i].count;
}
知道为什么会这样吗?有没有不同的方法来添加它们?我一无所知。
编辑:
忘记提到这个了。当我有断点并检查第一次迭代本身时,它不会加起来。 例如。如果 additionalCell[0].count 是 10。那么 currentPixelTotalCell.count 的值曾经只有在第一次迭代中执行了内线之后才为 null。
如果结果是 null
那么
- 要么
currentPixelTotalCell.count
是null
- 一些additionalCell[i].count是
null
确保两个 null
都在控制之下
// currentPixelTotalCell.count is not null
currentPixelTotalCell.count = 0;
for(int i = 0; i < 5; i++)
{
// if additionalCell[i].count is null treat it as 0
currentPixelTotalCell.count += additionalCell[i].count ?? 0;
}
您可以尝试使用 Linq 作为替代方法:
currentPixelTotalCell.count = additionalCell
.Take(5) // if you want take just first 5 items
.Sum(item => item ?? 0);
将您的内部循环更改为:
currentPixelTotalCell.count += (additionalCell[i].count ?? 0);
避免在右手值之一为空的情况下将总数设置为空。
我猜结果是 null
因为其中一个值是 null
.
怎么样:
currentPixelTotalCell.count += additionalCell.Select(x => x.count)
.Where(x => x.HasValue)
.Sum();
或
currentPixelTotalCell.count += additionalCell.Sum(x => x.count ?? 0);
不要忘记在某处初始化 currentPixelTotalCell.count
或用简单的赋值 =
替换 +=
。
难道是需要先将currentPixelTotalCell.count
变量初始化为0?
currentPixelTotalCell.count = 0;
for(int i = 0; i < 5; i++)
{
currentPixelTotalCell.count += additionalCell[i].count;
}
或者您可能必须检查 AdditionalCell 对象中的空值?
for(int i = 0; i < 5; i++)
{
currentPixelTotalCell.count += (additionalCell[i].count ?? 0)
}
有一个名为 .GetValueOrDefault()
的方法,它将为您提供 Nullable<T>
的默认值。如果值为 null
:
0
for(int i = 0; i < 5; i++)
{
currentPixelTotalCell.CmvF =currentPixelTotalCell.CmvF.GetValueOrDefault() + additionalCell[i].CmvF.GetValueOrDefault();
}
访问前需要将currentPixelTotalCell.count初始化为0
记住“a += b”只是“a = a + b”的语法糖。
因为 a 为 null,您实际上是在执行“a = null + b”并且 null 加上某项等于 null。
同样因为同样的约束,需要保证右边的值也不为null。在您的情况下,更简单的方法是简单地使用 GetValueOrDefault 方法。
综上所述,您的最终解决方案应该类似于:
currentPixelTotalCell.count = 0;
for(int i = 0; i < 5; i++)
{
currentPixelTotalCell.count += additionalCell[i].count.GetValueOrDefault();
}