469,943 Members | 2,576 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,943 developers. It's quick & easy.

Overriding GetHashCode ..

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?

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #1
1 1761
Anders Borum <an****@sphereworks.dk> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Rafael Veronezi | last post: by
4 posts views Thread by David Hoffer | last post: by
7 posts views Thread by Avin Patel | last post: by
5 posts views Thread by Stoyan | last post: by
3 posts views Thread by pagates | last post: by
3 posts views Thread by MuZZy | last post: by
8 posts views Thread by Kenneth Baltrinic | last post: by
5 posts views Thread by Metaman | last post: by
28 posts views Thread by Tony Johansson | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.