Both User.ID and Group.ID are Int16 and immutable and I want to generate an optimal HashCode.
This is the equality
public override bool Equals(Object obj)
{
//Check for null and compare run-time types.
if (obj == null || GetType() != obj.GetType()) return false;
UserGroup item = (UserGroup)obj;
return (User.ID == item.User.ID && Group.ID == item.Group.ID);
}
What would be an optimal GetHashCode. Right now I am using the following but only because I saw it as an example. The primary use of the Object is in a HashSet and that HashSet gets a lot of .Select(x => x.User.ID = y) or .Select(x => x.Group.ID = y).
public override int GetHashCode() { return (int)User.ID ^ (int)Group.ID; }