✅ Can my Equals(IEnumerable<T>? x, IEnumerable<T>? y) be made much simpler?

You may have any tricks to simplify the following.
public bool Equals(IEnumerable<T>? x, IEnumerable<T>? y)
{
if (x is null) return y is null;
if (y is null) return false;
return x.SequenceEqual(y);
}
public bool Equals(IEnumerable<T>? x, IEnumerable<T>? y)
{
if (x is null) return y is null;
if (y is null) return false;
return x.SequenceEqual(y);
}
3 Replies
Thinker
Thinker10mo ago
You can turn this into a single boolean expression, but it's debatable whether it's any more readable.
(x is null && y is null) || (x is not null && y is not null && x.SequenceEqual(y))
(x is null && y is null) || (x is not null && y is not null && x.SequenceEqual(y))
Judes
Judes10mo ago
absolutely disgusting but:
return (x, y) is (not null, not null) && x.SequenceEqual(y);
return (x, y) is (not null, not null) && x.SequenceEqual(y);
Thinker
Thinker10mo ago
That doesn't account for both being null Could be a switch expression tho
(x, y) switch
{
(null, null) => true,
(not null, not null) => x.SequenceEqual(y),
_ => false
}
(x, y) switch
{
(null, null) => true,
(not null, not null) => x.SequenceEqual(y),
_ => false
}
Want results from more Discord servers?
Add your server