I have a simple class, which holds a list of points:
public class PointCollection {
public List<PointModel> list;
public PointCollection()
{
list = new List<PointModel>();
}
public override int GetHashCode()
{
int hash = 13;
hash = (hash * 7) + list.GetHashCode();
return hash;
}
}
Here's how looks PointModel class:
public class PointModel
{
public float x = 0f;
public float y = 0f;
public PointModel()
{
x = y = 0;
}
public PointModel(float _x, float _y)
{
x = _x;
y = _y;
}
public override int GetHashCode()
{
int hash = 13;
hash = (hash * 7) + x.GetHashCode();
hash = (hash * 7) + y.GetHashCode();
return hash;
}
}
However, GetHashCode from PointCollection doesn't work properly. It always returns a different value and GetHashCode from PointModel is never called.
To have this working I have to explicitly iterate through a list and call GetHashCode for each point:
public override int GetHashCode()
{
int hash = 13;
for (int i = 0; i < list.Count; i++)
{
hash = (hash * 7) + list[i].GetHashCode();
}
return hash;
}
Why is that? I have different classes with similar configurations and it works fine, but not in this case.