[Mono-list] Dictionary<> key'ed by Decimal works differently between Mono and .NET

Robert Jordan robertj at gmx.net
Thu Aug 4 06:04:59 EDT 2011


On 04.08.2011 11:09, Mike Christensen wrote:
> I'm trying to get my unit tests to pass so I can try out my code on
> Mono, however I ran into some different behavior using the
> Dictionary<>  class.  I've written a stand-alone test case to
> illustrate the problem.
>
> http://pastie.org/2318806
>
> The code will print out "True" on .NET and "False" on Mono.  I believe
> this is because I add the key as 0.5m to the dictionary, but I try to
> look it up with 0.500m.  .NET will see these values as equal, Mono
> will not.
>
> Technically the numbers are equal, though they have different levels
> of precision.  However, I'm of the opinion that Mono should emulate
> .NET where-ever possible.

Well, MS.NET is inconsistent. It returns "false" in all cases
below, which means that it performs some insane magic inside
Dictionary, something that cannot be reproduced even using
its own comparer.

If you want this fixed, then you should provide some sort
of evidence (a document/blog/etc. that describes MS.NET's
behavior).

using System;
using System.Collections.Generic;

class Test
{
	static void Main ()
	{
		decimal a = 10.5m;
		decimal b = Math.Round (10.5m - 10, 3);
		Console.WriteLine (a == b);

		var map = new Dictionary<decimal, string>();
		var c = map.Comparer;
		Console.WriteLine (c.Equals (a, b));
		Console.WriteLine (c.GetHashCode (a) == c.GetHashCode (b));
	}
}

Robert



More information about the Mono-list mailing list