Breakpoint in GetHashCode of IEqualityComparer + GroupBy not hit

Viewed 323

Code example:

 var positions = new List<Position>();

 for (int i = 0; i < 20; i++)
 {
     positions.Add(new Position { Code = "A", Value = i });
     positions.Add(new Position { Code = "A", Value = i });
 }
 var test = positions.GroupBy(p => p, new PositionComparer());

public class Position
{
    public string Code;
    public int Value;
}
public class PositionComparer : IEqualityComparer<Position>
{
    public bool Equals(Position x, Position y)
    {
        return x.Code == y.Code && x.Value == y.Value;
    }
    public int GetHashCode(Position pos)
    {
        unchecked
        {
           int hash = 17;
           hash = hash * 31 + pos.Code.GetHashCode();
           hash = hash * 31 + pos.Value;
           return hash;
         }
     }
 }

I have a breakpoint in GetHashCode (and in Equals).
The breakpoint is not hit during the GroupBy, why not?

1 Answers
Related