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

Jamie Wyton
Jamie Wyton
3,011 Points

Two objects with the same hashcode?!

How can two objects be present in the same memory location?

7 Answers

Steven Parker
Steven Parker
231,268 Points

Two objects cannot occupy the same memory location or index.

A hash mechanism that is not guaranteed to generate a unique value cannot be used as a "memory location" or unique key. It would be necessary to augment the mechanism to guarantee a unique value.

Otherwise, the hash code might be used as only part of the unique key, and combined with some other factors to make the final key unique.

Say you doing a game called TreehouseDefense and you want to override GetHashCode for Location on a Map.

Wouldn't it be easier to just return an GetHashCode method as their Locations? Example, when X is 5 and Y is 6. Why not return 56 and not include math formulas?

Another example, X is 2 and Y is 6 , then return 26?

Steven Parker
Steven Parker
231,268 Points

That would work as long as the maximum dimensions only required 1 digit each. But by multiplying one dimension by one more than the maximum of the other dimensions, you could ensure a unique code for any size map.

rajan lagah
rajan lagah
2,539 Points

hash code is generated by hash function which is created such that minimum number of clashes like this occur. after this we also have technique to handle clashes .

1) eg if we have 11 elements and 11 places and place 5 is occupied and another element with hashcode 5 appear then it is shifted to 6 th position next time it will b shifted to 7.we have 11 elements 11 places so every thing will be stored we have some more techniques this is what introduction to algorithm book say .

Jamie Wyton
Jamie Wyton
3,011 Points

Yea I think where I was getting confused was I was thinking of the hash code as an actual memory hexadecimal location. So what is the relationship to the Equal() method then, why do we get a compilation warning of one or the other?

Steven Parker
Steven Parker
231,268 Points

Quoting from the MSDN Documentation:

If your overridden Equals method returns true when two objects are tested for equality, your overridden GetHashCode method must return the same value for the two objects.

Jamie Wyton
Jamie Wyton
3,011 Points

So if its one object to its own Hashcode(location), then surely it would be unique anyways?

Steven Parker
Steven Parker
231,268 Points

It might be, depending on how the hashcode relates to the object contents.

Jamie Wyton
Jamie Wyton
3,011 Points

can you give me an example of a clash that would occur from generic hash codes, I'm still not seeing what the real point of it all is for?

Steven Parker
Steven Parker
231,268 Points

I'm not sure what you mean by "generic", but let's imagine the hash code is generated by using the location and the formula "X + Y". So in that case, the point at 5,2 would have the exact same code as the one at 3,4 and there would be a clash.

rajan lagah
rajan lagah
2,539 Points

their is function to generating hashcode for example and just for example

function hash( String s){ //sum of ascii value of character of string }

hash(12) == ascii(1)+ascii(2) == 49+50 ==99; hash(c) == ascii(c) ==99;

hash(12)==hash(c); which is a clash

is this helpful ?