I am currently reading in Troelsen's book C# and the .NET 4.5 framework. There is a section in the book where he has an example of overriding
public virtual int GetHashCode(); // Defined in System.Object
He says (the following quotation is from Troelsen's book):
Given that the String class already has a solid hash code algorithm that is using the character data of the String to compute a hash value, if you can identify a piece of field data on your class that should be unique for all instances (such as a Social Security number), simply call GetHashCode() on that point of field data.
Basically what he says is that a certain class has a member (automatic read-only property)
public string SSN {get; }
and every instance of that class is going to have a unique string value. Now, under the assumption that
// s1 and s2 are strings
s1.GetHashCode() != s2.GetHashCode(); // Assumption: If this true then s1 == s2 is true
his reasoning would be valid. However, when I read on String.GetHashCode():
If two string objects are equal, the GetHashCode method returns identical values. However, there is not a unique hash code value for each unique string value. Different strings can return the same hash code.
I think you see where I am going with this. I guess it's me who's missing something, if so, please point me in the right direction.
Thanks!