Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2004

Tip: Looking for answers? Try searching our database.

Should GetHashCode return a nonnegative value

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Niko Korhonen - 09 Aug 2004 13:11 GMT
The return type for the .NET framework System.Object's method
GetHashCode is a signed integer. However, I've noticed that especially
when doing some XOR magic, the hash code tends to be a large negative
number.

My question is that should user-defined GetHashCode method always return
a nonnegative number? I think that the most common use for hash codes is
something like this:

#  hashTable[obj.GetHashCode() % hashTable.Length] = obj;

But when GetHashCode returns a negative number, the result of the modulo
calculation is negative and therefore the code throws array index exception.
Stu Smith - 09 Aug 2004 15:17 GMT
> The return type for the .NET framework System.Object's method
> GetHashCode is a signed integer. However, I've noticed that especially
[quoted text clipped - 9 lines]
> But when GetHashCode returns a negative number, the result of the modulo
> calculation is negative and therefore the code throws array index exception.

The system Hashtable casts the int to a uint before performing the module,
so any value (positive, negative or zero) is a valid hashcode.

Stu

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.