How do I ensure two different instances of a class have the same hash code? So when one of them is in a HashSet
, the Contains
function returns true
.
Here is my code:
public class Position
{
public int row, col;
public override bool Equals(object obj)
{
return ((Position)obj).row == row && ((Position)obj).col == col;
}
}
And here is how I use it:
HashSet<Position> hash = new HashSet<Position>();
Position position = new Position(3, 1);
Position position2 = new Position(3, 1);
hash.Add(position);
Console.WriteLine(hash.Contains(position2));