I have a struct that overrides the Equals()
method and the compiler complains about GetHashCode()
not being overridden.
My struct:
private struct Key
{
...
public override int GetHashCode()
{
return ?;
}
public int FolderID;
public MyEnum SubItemKind;
public int SubItemID;
}
What is the right way to implement the GetHashCode()
method?
a)
return FolderID ^ SubItemKind.GetHashCode() ^ SubItemID;
or b)
return FolderID.GetHashCode() ^ SubItemKind.GetHashCode() ^ SubItemID.GetHashCode();
System.Int32.GetHashCode()
performs a simplereturn this
(at least in mscorlib v4), so your two code snippets would be equivalent anyway. – Floorwalkerstruct
. The core of the problem is combining multiple hash codes into one, which is explained in the top answer to the "What is the best algorithm for an overridden System.Object.GetHashCode?" question. – Phocaea