Click here to Skip to main content
12,815,616 members (40,302 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


Posted 14 Oct 2011

Using GetHashCode correctly

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


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

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


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 | Terms of Use | Mobile
Web01 | 2.8.170308.1 | Last Updated 16 Oct 2011
Article Copyright 2011 by Panos Rontogiannis
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid