Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C#

Using GetHashCode correctly

Rate me:
Please Sign up or sign in to vote.
3.92/5 (5 votes)
3 Oct 2011CPOL2 min read 31.6K   2  
It may not always be obvious what makes a good source for a HashCode.

Alternatives

Members may post updates or alternatives to this current article in order to show different approaches or add new features.

Please Sign up or sign in to vote.
16 Oct 2011Panos Rontogiannis
For reference types, the hash is based on an internal counter that is incremented every time a descendant of System.Object is created. This happens in System.Object's constructor.For value types (structs), the hash is based on the first field. So if the first field is immutable, then you don't...
Please Sign up or sign in to vote.
12 Oct 2011Qwertie
Assuming you are writing a class (not a struct), you can basically get a fixed random hashcode simply by NOT overriding GetHashCode() but inheriting the implementation from System.Object. Unfortunately the System.Object implementation is not documented in the standard .NET documentation[^] but I...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions