I have created an object, say details. I then assign: int x = details.GetHashCode();
Later in the program, I would like to access this object using the integer x. Is there a way to do this in C#?
Many thanks
Paul
No:
You could create (say) a Dictionary<int, Details> and use the hash code as the key - but I'd strongly recommend that you didn't do that.
Any reason you don't want to just keep a reference to the object instead of the hash code?
Store the integer and just check the Hashcode again.
Note that in C# the Hashcode is not guarenteed unique. If you are dealing with a few million objects, you can and will run across duplicate hashes with the default implementation very easily.
http://msdn.microsoft.com/en-us/library/system.object.gethashcode.aspx
"The default implementation of the GetHashCode method does not guarantee unique return values for different objects."
A hash code isn't really meant to be consumed directly. Its main purpose is so that the item can be used as a key in a collection. It is then the collections responsibility to map the hash back to the object. basically it lets you do something like Dictonary<details, myClass2> which would be much harder if GetHashCode wasn't implemented... but the function isn't a whole lot of use unless you are implementing your own collection or equality operator.