Enumerable.Sum()溢出

Han*_*ank 12 .net c# linq decompiling

嘿,我正在使用Enumerable.Sum()LINQ 的扩展方法来计算哈希码,并且OverflowExceptions在代码变大时遇到问题.我尝试将电话放在一个unchecked区块中,但这似乎没有帮助.

该方法的MSDN文档说,如果值太大,它将抛出,但我检查了反射器,这就是:

public static int Sum(this IEnumerable<int> source) {
    if (source == null) {
        throw Error.ArgumentNull("source");
    }
    int num = 0;
    foreach (int num2 in source) {
        num += num2;
    }
    return num;
}
Run Code Online (Sandbox Code Playgroud)

根据这个反编译,我希望它可以溢出或不取决于调用代码的上下文.为什么它会溢出,我怎么能让它停止?

Jar*_*Par 9

代码确实在C#checked块中执行.问题是反射器没有正确地反编译checked块,而是将它们显示为正常的数学运算.您可以通过创建一个选中的块,编译代码然后在反射器中反编译来自行验证.

您还可以通过查看IL而不是反编译的C#代码来验证这一点.而不是添加IL操作码,您将看到添加发生在add.ovf.这是溢出时抛出的add版本

L_001a: callvirt instance !0 [mscorlib]System.Collections.Generic.IEnumerator`1<int32>::get_Current()
L_001f: stloc.1 
L_0020: ldloc.0 
L_0021: ldloc.1 
L_0022: add.ovf <-- This is an overflow aware addition
L_0023: stloc.0 
L_0024: ldloc.2 
Run Code Online (Sandbox Code Playgroud)

没有办法让这个特殊的方法不抛出溢出.您最好的选择如下

  1. 切换到更大的类型,如 long
  2. 编写自己的Sum版本,不使用检查添加


rav*_*oli 7

我为通用枚举写了这个函数.我很想听听有关它的任何评论.

public static int SequenceHashCode<T>(IEnumerable<T> seq)
{
    unchecked
    {
        return seq != null ? seq.Aggregate(0, (sum,obj) => sum+obj.GetHashCode()) : 0;
    }
}
Run Code Online (Sandbox Code Playgroud)