在以下代码中,Resharper声称表达式currMax == null始终为false.
public Read Read1
{
get
{
return Reads.Where(read => !read.IsIndex)
.Aggregate((Read)null, (currMax, read) => currMax == null || read.Length > currMax.Length ? read : currMax);
}
}
当对Aggregate的调用中的lambda表达式的第一次迭代执行时,我希望currMax == null为真,因为(Read)null是种子. Resharper知道上游Where在生成的IEnumerable中需要非空的Read对象,因为我正在访问Read类的IsIndex属性. resharper只是不考虑Aggregate的种子参数吗?
编辑:我在这里提交了一份错误报告https://youtrack.jetbrains.com/issue/RSRP-443055
最佳答案 看起来ReSharper是错误的,从ILSpy中的Aggregate的反汇编来看.
public static TAccumulate Aggregate<TSource, TAccumulate>(
this IEnumerable<TSource> source,
TAccumulate seed,
Func<TAccumulate, TSource, TAccumulate> func)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (func == null)
{
throw Error.ArgumentNull("func");
}
TAccumulate tAccumulate = seed;
foreach (TSource current in source)
{
tAccumulate = func(tAccumulate, current);
}
return tAccumulate;
}
清除删除空检查将导致空引用异常.