Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will string.GetHashCode() return negative value?

I tried with batch of random strings, all values I got are positive, but I wondering:

Will String.GetHashCode() return negative or 0?

Since the return value is int, so I guess it might be, so if it is the case, I have to change my logic.

If you have answer or have some official sources, please share

like image 814
Eric Yin Avatar asked Jan 31 '12 18:01

Eric Yin


2 Answers

Yes, it can return negative values.

You must not have any logic that works with GetHashCode() values.
GetHashCode() is not guaranteed to be unique and can change between builds.

GetHashCode() must be treated as an opaque token that can be combined with other hashes or modded out into hashtables.

like image 136
SLaks Avatar answered Oct 26 '22 23:10

SLaks


It can return negative value (based on msdn):
http://msdn.microsoft.com/en-us/library/system.string.gethashcode.aspx

like image 42
cichy Avatar answered Oct 26 '22 23:10

cichy