Skip to content

Add == and != operators to ValueTuple.cs #17954

@OzieGamma

Description

@OzieGamma

After the following discussion on https://github.com/dotnet/coreclr/issues/6520#issuecomment-236152874 we discovered ValueTuple did not implement the == and != operators.

I think this is an oversight, it looks pretty natural to me to write:

(name, age) = GetPersonTuple();
if ((name, age) == ("Jhon", 13))
{
    // ...
}

OR

tuple = GetPersonTuple();
if (tuple == ("Jhon", 13))
{
    // ...
}

Personally, I think that if the performance cost is not too high I might even compare variables using the tuple notation. It is easier to understand and to read than the && syntax:

var age = AskAge();
var name = AskName();

if((age, name) == (17, "Jhon"))
{
    // ...
}

instead of:

var age = AskAge();
var name = AskName();

if(age == 17 && name == "Jhon")
{
    // ...
}

This might overlap with the Tuple Patterns but I think those were removed from C# 7. Having to call .Equals seems very ugly to me.

tuple = GetPersonTuple();
if (tuple is ("Jhon", 13))
{
    // ...
}

Metadata

Metadata

Assignees

Labels

api-needs-workAPI needs work before it is approved, it is NOT ready for implementation

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions