C# Why can equal decimals produce unequal hash values?

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)