Description
The implementation of HashSetEqualityComparer<T>.Equals does not consider the Count of collections when they have different comparers. That means it's possible for two sets to compare equals when they have a different count of items. That leads to the implementation not being symmetric.
Reproduction Steps
var x = new HashSet<int>(new Comparer());
x.Add(1);
var y = new HashSet<int>(new Comparer());
y.Add(1);
y.Add(2);
var comparer = HashSet<int>.CreateSetComparer();
Console.WriteLine(comparer.Equals(x, y));
Console.WriteLine(comparer.Equals(y, x));
internal sealed class Comparer : IEqualityComparer<int>
{
public bool Equals(int x, int y) => x == y;
public int GetHashCode(int obj) => obj.GetHashCode();
}
Sharplab demonstration
Expected behavior
The expected behavior is to print
Actual behavior
This code prints:
Regression?
No
Known Workarounds
No response
Configuration
net6.0
Other information
No response
Description
The implementation of
HashSetEqualityComparer<T>.Equalsdoes not consider theCountof collections when they have different comparers. That means it's possible for two sets to compare equals when they have a different count of items. That leads to the implementation not being symmetric.Reproduction Steps
Sharplab demonstration
Expected behavior
The expected behavior is to print
Actual behavior
This code prints:
Regression?
No
Known Workarounds
No response
Configuration
net6.0Other information
No response