I'm trying to override GetHashCode
to ensure uniqueness, since i use the instances as keys in a dictionary:
IDictionary<Base, int> _counts = new Dictionary<Base,int>();
The two classes I have an issue with are:
class sealed First : Base
{
public MyEnum1 value;
public ExtrasEnum extras;
public override int GetHashCode()
{
unchecked
{
return ((int)value* 397) ^ (int)extras;
}
}
//Other stuff
}
class sealed Second : Base
{
public MyEnum2 value;
public ExtrasEnum extras;
public override int GetHashCode()
{
unchecked
{
return ((int)value* 397) ^ (int)extras;
}
}
//Other stuff
}
However. The issue is that when the value
and extras
int values become the same, then the hash codes will be equal.
The calculation was the recommended one by Resharper.
How do i ensure that the hashcodes for theese classes does not be come the same?
Just mix it up a little with another prime number, or?
EDIT:
Just to explain. I need that if to instances of First
has the same value
and extras
values, then these two instances must be considered the same, but if an instance of First
, and a instance of Second
have the same int values of value
and extras
, then these must not be considered the same.
I'm not looking into performance, but just to ensure that same class instances are equal, and different class instances are different.