I'm seeing different behavior between using .Equals and == between two of .NET 4.0's new Tuple<> instances. If I have overridden Equals on the object in the Tuple<> and call .Equals on the Tuples the override of Equals will be called. If I use == on the Tuples the override of Equals is not called. Is that by design and does it make sense?
EDIT: From answers and comments I can tell I'm not being clear. I know Tuple<> is a reference type and that for reference types == will check identity (ReferenceEquals). But, should Tuple<> override == to check equality of the objects it contains? For consistency, probably not.
For example if I have a simple object
public class NameAndNumber
{
public int Number { get; set; }
public string Name { get; set; }
public override bool Equals(object obj)
{
if (obj is NameAndNumber)
{
NameAndNumber other = (NameAndNumber)obj;
return Number == other.Number && Name == other.Name;
}
return false;
}
}
and then I do something like this:
Tuple<NameAndNumber, NameAndNumber> left = new Tuple<NameAndNumber, NameAndNumber>(
new NameAndNumber { Name = "one", Number = 1 },
new NameAndNumber { Name = "two", Number = 2 });
Tuple<NameAndNumber, NameAndNumber> right = new Tuple<NameAndNumber, NameAndNumber>(
new NameAndNumber { Name = "one", Number = 1 },
new NameAndNumber { Name = "two", Number = 2 });
bool operatorResult = left == right;
bool equalsResult = left.Equals(right);
Console.Out.WriteLine("operatorResult = {0} equalsResult = {1}",
operatorResult, equalsResult);
I get operatorResult = false equalsResult = true
Should I be expecting that?
I know the implementation of Equals on NameAndNumber isn't "right" it's just simplified sample code.
I have also tried implementing IEquatable, ==, !=, and GetHashCode. Same results.