[Mono-list] Differences in GetHashCode() of Type between .NET and Mono

Robert Jordan robertj at gmx.net
Mon Dec 8 09:58:40 UTC 2014


On 08.12.2014 09:21, Hüning, Christian wrote:
>
> However I found that Mono and .NET produce different hashcodes for the same
> type from the same DLL. On second thought, this is very clear to me, since
> up to recently we could have no idea how the GetHashCode() implementation in
> .NET looks like.
>
>
>
> Now for my algorithm this is a little showstopper. So I’ve got two
> questions:
>
> A)     Is there another way to achieve my scenario above?
>

You're not supposed to rely on what GetHashCode () is returning.
Neither in MS.NET (where its result might differ between MS.NET
versions), nor in Mono.

See 
http://msdn.microsoft.com/en-us/library/system.object.gethashcode%28v=vs.110%29.aspx

The only reliable and valid relations are:

Object.ReferenceEquals(a, b)
     => a.GetHashCode() == b.GetHashCode()

a.Equals(b)
     => a.GetHashCode() == b.GetHashCode()

> B)      Are there plans to use .NET’s hashcode implementation in Mono now
> that it’s available?

There are no such plans. Changing Object/ValueType.GetHashCode () or
RuntimeHelpers.GetHashCode () won't be enough anyway. Every
overridden GetHashCode () would need a review/change...

And since sane application don't rely on this, there isn't
much motivation for such changes ;)

Robert




More information about the Mono-list mailing list