Overriding GetHashCode ..

  • Thread starter Thread starter Anders Borum
  • Start date Start date
A

Anders Borum

Hello!

I have a framework where all objects are uniquely identified by a GUID
(Global Unique Identifier). The objects are used in conjunction with lots of
hashtables and I was thinking, that overriding the GetHashCode base method
(inherited from object) was a good idea.

public abstract class CmsObjectNode : CmsObject, IXml
{
private Guid cmsObjectID = Guid.NewGuid();

public Guid CmsObjectID
{
get { return this.cmsObjectID; }
}

public override int GetHashCode()
{
return this.cmsObjectID.GetHashCode ();
}
}

Comments?
 
Anders Borum said:
I have a framework where all objects are uniquely identified by a GUID
(Global Unique Identifier). The objects are used in conjunction with lots of
hashtables and I was thinking, that overriding the GetHashCode base method
(inherited from object) was a good idea.

public abstract class CmsObjectNode : CmsObject, IXml
{
private Guid cmsObjectID = Guid.NewGuid();

public Guid CmsObjectID
{
get { return this.cmsObjectID; }
}

public override int GetHashCode()
{
return this.cmsObjectID.GetHashCode ();
}
}

Comments?

Without overriding Equals, it's a very bad idea. Basically, you need to
make Equals and GetHashCode tie up together. If the CmsObject exposes
any properties used in testing for equality, those should probably be
part of the hash too.
 
Back
Top