Hey, I\'m using the Enumerable.Sum()
extension method from LINQ to compute hash codes, and am having a problem with OverflowExceptions
when the cod
The code is indeed executing in a C# checked
block. The problem is that reflector doesn't properly decompile checked
blocks and instead shows them as normal mathmatical operations. You can verify this yourself by creating a checked block, compiling the code and then decompiling it in reflector.
You can also verify this by looking at the IL instead of the decompiled C# code. Instead of the add IL opcode you'll see that the addition occurs with add.ovf. This is the version of add that throws on overflows
L_001a: callvirt instance !0 [mscorlib]System.Collections.Generic.IEnumerator`1::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
There is no way to get this particular method to not throw on overflow. Your best options are the following
long