RobV
RobV

Reputation: 28636

Can the GetHashCode() method of a string return zero?

When obtaining the hash code of a string using the GetHashCode() method is there any chance that it can return zero or does the algorithm used guarantee otherwise?

The reason I'm asking is that I have a use case where I need to invent a hash for a null string and I was thinking of using zero rather than hashing some constant string. If I do this how likely am I to get a collision (barring the obvious fact that collisions are always possible)

Upvotes: 4

Views: 3280

Answers (3)

Hans Passant
Hans Passant

Reputation: 941218

This is risky, a null string can get coerced to an empty string. For example:

        string nullstr = null;
        string notnull = nullstr + nullstr;

A bit outlandish perhaps, but you'll have a helluvatime debugging the problem when it happens. The simple solution is to use string.Empty.GetHashCode(), there's no requirement whatsoever that the hash code is unique.

Upvotes: 1

shf301
shf301

Reputation: 31394

There's no way to definitively answer that. The behavior of String.GetHashCode() is documented as being undefined and subject to change between framework versions and to be different between 32bit and 64 bit systems.

If you chose some other value you may be just as likely to have a collision. Zero would be a pretty reasonable default.

The Nullable.GetHashCode() returns 0 if it stores a null value so return a hash code for zero has some precedent.

Upvotes: 5

leppie
leppie

Reputation: 117220

GetHashCode() only requires the hashcode to be consistent. It does not need to be unique. So zero is a valid but very naive hash value :)

Obviously, this will cause many collisions in a hashtable.

As for a string hashcode, I guess in some conditions that will be possible.

Upvotes: 2

Related Questions