Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# Intermediate C# System.Object Object.GetHashCode

Mark Blanford
Mark Blanford
427 Points

If the hashcode is the memory address by default how can it also possibly *not* be unique?

Why would GetHashCode() need to be overridden under normal circumstances if by default it returns the memory address? Two different objects can't have the same memory address, so is it not inherently unique? Or is this in case a different object has had its GetHashCode method altered so that it happens to return something that looks identical to an existing memory address (but is in fact just an integer and a coincidence)? This is very confusing!

3 Answers

Steven Parker
Steven Parker
231,275 Points

The issue is that you don't want the hash code to always be unique. In particular, if two things are equal then they must return the same value for GetHashCode().

So when you change how things are determined to be equal, you're expected to also change the hash method so equal items will return the same thing.

Richard Külling
Richard Külling
9,465 Points

That explanation needs to be in the video imo.

Mark Blanford
Mark Blanford
427 Points

Got it, that makes sense. Thank you!