Bug 45614 - SortedList keys are behaving differently in .net clr and mono
Summary: SortedList keys are behaving differently in .net clr and mono
Status: RESOLVED FEATURE
Alias: None
Product: Class Libraries
Classification: Mono
Component: General ()
Version: 4.6.0 (C8)
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2016-10-18 07:36 UTC by Roman Kuznetsov
Modified: 2016-10-19 21:24 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FEATURE

Description Roman Kuznetsov 2016-10-18 07:36:06 UTC
```
let xs : byte [] = [|99uy; 117uy; 115uy; 116uy; 111uy; 109uy; 101uy; 114uy; 58uy; 80uy; 75uy; 74uy; 195uy; 134uy; 82uy; 73uy; 78uy; 86uy|]
let ys : byte [] = [|99uy; 117uy; 115uy; 116uy; 111uy; 109uy; 101uy; 114uy; 58uy; 80uy; 75uy; 74uy; 65uy; 69uy; 82uy; 73uy; 78uy; 86uy;|]
let x = Encoding.UTF8.GetString (xs)
let y = Encoding.UTF8.GetString (ys)
let sl = new SortedList<string, byte[]>()
sl.Add(x, xs)
sl.ContainsKey(y) // produces false on .Net clr, but true in mono
sl.Add(y, ys)
```

`sl.ContainsKey(y)` produces false on .Net clr, but true in mono. This does work correctly if `Dictionary<string, byte[]>` is used.
Comment 1 Marek Safar 2016-10-19 21:24:52 UTC
I am using following C# repro but it behaves same on Mono 4.8 and .net 4.6

using System.Collections.Generic;
using System.Text;
using System;

class X
{
	public static void Main ()
	{
		var xs = new byte [] { 99, 117, 115, 116, 111, 109, 101, 114, 58, 80, 75, 74, 195, 134, 82, 73, 78, 86 };
		var ys = new byte [] { 99, 117, 115, 116, 111, 109, 101, 114, 58, 80, 75, 74, 65, 69, 82, 73, 78, 86 };
		var x = Encoding.UTF8.GetString (xs);
		var y = Encoding.UTF8.GetString (ys);

		Console.WriteLine (x.CompareTo (y));

		var sl = new SortedList<string, byte []> ();
		sl.Add (x, xs);
		var res = sl.ContainsKey (y);
		Console.WriteLine (res);
		sl.Add (y, ys);
		return;
	}
}


0
True

Unhandled Exception:
System.ArgumentException: An entry with the same key already exists.
  at System.ThrowHelper.ThrowArgumentException (System.ExceptionResource resource) [0x0000c] in /Users/marek/git/mono/mcs/class/referencesource/System/compmod/system/collections/generic/throwhelper.cs:63 
  at System.Collections.Generic.SortedList`2[TKey,TValue].Add (TKey key, TValue value) [0x00034] in /Users/marek/git/mono/mcs/class/referencesource/System/compmod/system/collections/generic/sortedlist.cs:181 
  at X.Main () [0x00070] in /Users/marek/Projects/console88732/Program.cs:20