Bug 5384 - Tuple.Equals throws exception
Summary: Tuple.Equals throws exception
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: 2.10.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-05-29 03:33 UTC by cpdigger
Modified: 2012-05-29 05:17 UTC (History)
2 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 FIXED

Description cpdigger 2012-05-29 03:33:01 UTC
If we try to compare a Tuple<T1, T2> with some other type we always got the Exception:

System.ArgumentException: Value does not fall within the expected range.
  at System.Tuple`2[System.Int32,System.String].System.Collections.IStructuralEquatable.Equals (System.Object other, IEqualityComparer comparer) [0x00000] in <filename unknown>:0
  at System.Tuple`2[System.Int32,System.String].Equals (System.Object obj) [0x00000] in <filename unknown>:0
  at TupleTest.MainClass.Main (System.String[] args) [0x0001a] in /TupleTest/Main.cs:12

public static void Main (string[] args)
{
	Tuple<int, string> t1 = Tuple.Create(42, "test");
	Tuple<string, int> t2 = Tuple.Create("test", 42); // string t2 = "123"; 
			
	Console.WriteLine ("Result: {0}", t1.Equals(t2));
}

It does not matter witch type "t2" is.
--------------------------------------------------------

We believe that the problem is in the method: 

bool IStructuralEquatable.Equals (object other, IEqualityComparer comparer)
{
	Tuple<T1, T2> tuple = other as Tuple<T1, T2>;
	if (tuple != null)
	{
		return comparer.Equals (this.item1, tuple.item1) && comparer.Equals (this.item2, tuple.item2);
	}
	if (other == null)
	{
		return false;
	}
	throw new ArgumentException (); <--- !!!THAT`S WRONG!!!
}

in .Net Framework implemantations looks like this:

bool IStructuralEquatable.Equals (object other, IEqualityComparer comparer)
{
	if (other == null)
	{
		return false;
	}	
        Tuple<T1, T2> tuple = other as Tuple<T1, T2>;
	if (tuple == null)
	{
		return false;
	}
        return comparer.Equals (this.item1, tuple.item1) && comparer.Equals (this.item2, tuple.item2);
}

Steps to reproduce the problem:
Just run the little main-method above

Actual Results:
System.ArgumentException: Value does not fall within the expected range.
  at System.Tuple`2[System.Int32,System.String].System.Collections.IStructuralEquatable.Equals (System.Object other, IEqualityComparer comparer) [0x00000] in <filename unknown>:0
  at System.Tuple`2[System.Int32,System.String].Equals (System.Object obj) [0x00000] in <filename unknown>:0
  at TupleTest.MainClass.Main (System.String[] args) [0x0001a] in /TupleTest/Main.cs:12

Expected Results:
Console Output: "Result: false"

How often does this happen? 
Every time

Additional Information:
We tried to reproduce this with the .Net Framework under Win7 -> it works.
Comment 1 Marek Safar 2012-05-29 05:12:47 UTC
Fixed in Mono 2.11
Comment 2 cpdigger 2012-05-29 05:16:58 UTC
Hi Marek,

thanks a lot for your fast reply. Did you fix it after the Bug-Report or was it already fixed for 2.11?

greetings from Germany
Comment 3 Marek Safar 2012-05-29 05:17:41 UTC
It is already fixed in released 2.11