nicecatch
nicecatch

Reputation: 1737

ValueTuple.Equals doesn't return true

Given what is written in the documentation ( https://learn.microsoft.com/en-us/dotnet/api/system.valuetuple.equals?view=netframework-4.7.1 ) it seems that ValueTuple.Equals should always return true

Does it mean it will return true even if the tuples have different value in their fields?

I'm trying to test it but I cannot hit the Console.Writeline with this simple code:

if((1,2).Equals((2,1)))
{
    Console.WriteLine("It's true");
}

Is there any caveat I should be aware of?

I've tested with c# versions from 7.0 to 7.3

Upvotes: 2

Views: 298

Answers (3)

Uladz
Uladz

Reputation: 1968

As you can see from source ValueTuple.Equals always returns true indeed, the same is written in docs. But (1, 2) has type ValueType<int, int> which has different Equals logic, see here.

Upvotes: 2

JamesFaix
JamesFaix

Reputation: 8645

Tuple equality is memberwise, so (1,2) == (1,2) but (1,2) != (2,1).

Upvotes: -2

Evk
Evk

Reputation: 101453

You are reading documentation of non-generic ValueTuple. This one has no fields and represents an "empty" ValueTuple, so of course one empty tuple is always equal to another empty tuple.

In your example code you are using generic ValueTuple<T1, T2>, and this documentation article is not related to it. Here is relevant Equals method documentation.

Upvotes: 16

Related Questions