a class has an ID property and this property gets value from a primary key column of an SQL table.
Is it a good practice if I write
public override int GetHashCode()
{
return this.ID + GetType().GetHashCode();
}
into my class? (Equals overrided already on the same way.)
Why would you particularly want to include the type in the hashcode? I can see how that could be useful if you had a lot of different types of object with the same ID in the same map, but normally I’d just use
Note that ideas of equality become tricky within inheritance hierarchies – another reason to prefer composition over inheritance. Do you actually need to worry about this? If you can seal your class, it will make the equality test easier as you only need to write:
(You may well want to have a strongly-typed Equals method and implement IEquatable.)