There's a lot of information on the internet about how to override GetHashCode() when Equals is overriden. But, all these examples are about classes that contain a few fields that can generate a hash. What I'm trying to find is a good GetHashCode implementation for a base class I use for all my business logica layer objects. This class, called BusinessLogica, contains a ToString() implementation, some basic functionality for my framework and the following Equals override:
public override bool Equals(object obj)
{
bool retValue;
if (obj is BusinessLogica && this.GetType() == obj.GetType())
{
retValue = this.ID == ((BusinessLogica)obj).ID;
}
else
{
retValue = false;
}
return retValue;
}
Now, what I've done so far is when I need an object that extends this BusinessLogica and which I use as a key in a dictionary, I override GetHashCode in this particular class and return ID. I could also use this implementation in the BusinessLogica baseclass. Is this 'safe'? I've also seen examples where ToString().GetHashCode() is returned.
What would be wise to use? Or is a GetHashCode on this level not usable and should I really override it in every of my BusinessLogica classes?