C# Why can equal decimals produce unequal hash values?

前端 未结 6 1574
后悔当初
后悔当初 2020-12-08 06:43

We ran into a magic decimal number that broke our hashtable. I boiled it down to the following minimal case:

decimal d0 = 295.50000000000000000000000000m;
de         


        
6条回答
  •  一个人的身影
    2020-12-08 07:12

    To start with, C# isn't doing anything wrong at all. This is a framework bug.

    It does indeed look like a bug though - basically whatever normalization is involved in comparing for equality ought to be used in the same way for hash code computation. I've checked and can reproduce it too (using .NET 4) including checking the Equals(decimal) and Equals(object) methods as well as the == operator.

    It definitely looks like it's the d0 value which is the problem, as adding trailing 0s to d1 doesn't change the results (until it's the same as d0 of course). I suspect there's some corner case tripped by the exact bit representation there.

    I'm surprised it isn't (and as you say, it works most of the time), but you should report the bug on Connect.

提交回复
热议问题