Click here to Skip to main content
15,887,386 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Using GetHashCode correctly

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
16 Oct 2011CPOL 5.5K  
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...

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 have to overload GetHashCode for your type since the hash based on it will never change.


Source: Bill Wagner's Effective C# (first edition).

License

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


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

Comments and Discussions

 
-- There are no messages in this forum --