Do structs built into the .NET Framework always return the same hash code if the contents are equal? Specifically I am thinking about System.Drawing.Color and Size
Asked
Active
Viewed 167 times
-1
-
2You can expect them to behave as described in the [docs](http://msdn.microsoft.com/en-us/library/system.object.gethashcode.aspx). Don't rely on the hashes being consistent for longer than a single program execution though - or any of the other caveats listed on that page. And of course, objects that are different can also have the same hash code. – Blorgbeard Aug 19 '14 at 02:23
1 Answers
1
Yes.
Correctly implemented GetHashCode
returns the same value for items that are considered equal. All existing struct in .Net indeed implement such functionality to meet that expectation.
Notes
- if hash codes are equal it does not mean objects are equal.
- there is no requirement for hash code for the same "value" to stay the same outside single execution of an application. It is unlikely for simple types like
Int32
andSize
, but one should not rely on hash code. If you need consistent value - implement and use your own methods.
You can get more detailed discussion from Eric Lippert's Guidelines and Rules for GetHashCode.

Alexei Levenkov
- 98,904
- 14
- 127
- 179