Click here to Skip to main content
Click here to Skip to main content

Using GetHashCode correctly

, 16 Oct 2011
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...

Alternatives

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

3 Oct 2011
Rossl321
2 alternatives  
It may not always be obvious what makes a good source for a HashCode.
12 Oct 2011
Qwertie
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)

About the Author

Panos Rontogiannis
Software Developer
Greece Greece
No Biography provided

| | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 16 Oct 2011
Article Copyright 2011 by Panos Rontogiannis
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid