views:

78

answers:

1

Possible Duplicate:
How does Object.GetHashCode work when the GC moves an object?

The garbage collector in .NET moves objects, so the object's address is not stable. How is Object's GetHashCode() method implemented?

Thanks!

+3  A: 

The garbage collector assigns with each object an unique ID, and this is what the default GetHashCode implementation uses instead of the memory location.

Edit It's a bit more complex than that actually. Not want to mislead anyone, so you guys should prefer Jon Skeet's answer on the topic.

zneak
+1 both for the correct answer, as well as your link :)
rwmnau
I do not think that link has correct information, at least not with the latest version of .NET. When I call lock on an Object, I see its memory usage increase, consistent with it creating a sync block. But I do not see any increase in memory usage when I call GetHashCode, so I do not think it creates a sync block.
fish
@fish: that's not a very scientific way to test it, in my opinion. How do you know the memory increase is related to the sync block and not to whatever else .NET creates when you use Monitor.Enter?
zneak