Overriding GetHashCode

Den Delimarsky picture Den Delimarsky · Nov 21, 2010 · Viewed 55.5k times · Source

As you know, GetHashCode returns a semi-unique value that can be used to identify an object instance in a collection. As a good practice, it is recommended to override this method and implement your own.

My question is - do you override this method when working on custom objects? If so, what algorithm do you use to generate the unique ID?

I was thinking about generating a GUID and then getting integer data from that identificator.

Answer

Winston picture Winston · Dec 2, 2010

If you use resharper it can generate the GetHashCode(), Equals and operator method bodies for you.

Access this menu by pressing Alt+Insert.

http://www.jetbrains.com/resharper/webhelp/Code_Generation__Equality_Members.html