-
GetHashCode
Hi All,
Can someone explain in simple terms what's the use of HashCode in VB.Net and
what is the purpose of GetHashCode method of an object?
Thanks in advance.
Honey.
-
Re: GetHashCode
A hash algorithm generates a number based on a value (any data type). The
idea is that the number should be unique to the value so this hash value
can be used as a key for other algorithms. For example, it's faster to search
through collections for numbers than by using string comparisons, so you
can hash the string value, and look through the collection for the hash value
instead.
-Rob
"Honey" <honey@hotmail.com> wrote:
>Hi All,
>
>Can someone explain in simple terms what's the use of HashCode in VB.Net
and
>what is the purpose of GetHashCode method of an object?
>
>Thanks in advance.
>
>Honey.
>
>
-
Re: GetHashCode
Below the response I made in microsoft.public.dotnet.languages.vb :
This is pretty well explained in the documentation.
See for example
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfSystemObjectClassGetHashCodeTopic.asp
You may want also to check the Hashtable class.
The basic idea is to create an integer value from a key (could be for
example a string). You could use then this integer as an index into an array
to store the associated data. If you search later for this same key, you can
then use the same function that will give the same value and that will allow
to retrieve directly the element instead of testing each cell in turn for
the key value.
Collisions are generally taken into account (mutliple keys could give the
same integer value but still you'll have to search in a very reduced number
of cells instead of having to browse the whole data structure to find the
matching element).
Hope this helps
Patrice
"Honey" <honey@hotmail.com> a écrit dans le message news:
3ce0cf6b@10.1.10.29...
> Hi All,
>
> Can someone explain in simple terms what's the use of HashCode in VB.Net
and
> what is the purpose of GetHashCode method of an object?
>
> Thanks in advance.
>
> Honey.
>
>
-
Re: GetHashCode
"Rob Teixeira" <RobTeixeira@@msn.com> wrote:
>
>
>A hash algorithm generates a number based on a value (any data type). The
>idea is that the number should be unique to the value so this hash value
>can be used as a key for other algorithms.
Actually, hash numbers do not uniquely represent a value. Usually, it is
impossible for them to be unique. For example, think of how many permutations
a 255 character string has and compare that to the relatively few permutations
possible from a 32 bit integer. Instead, the goal is to generate a relatively
unique number from a given value.
Therefore, if two objects return the same hash number it is only HIGHLY LIKELY
that the two objects are equal (valuewise). That is still a far cry from
gauranteed equality, which is why the doc recommends overriding Equals too
if you override GetHashCode. When two objects return the same hash, you then
call Equals to confirm value equality.
For example, lets say you have two Customer objects that represent the same
customer: Homer Simpson. If you call GetHashCode on these objects they (by
definition) should return the same hash code.
However, if you have two Customer objects and they both return the same hash
code, you can make no claim about their equality. So you have to call Equals
to confirm equality.
Sorry Rob - you probably know all this. Its just that in my early days hash
numbers confused me too because they were explained to me as "unique values"
and I didn't see how that was possible. I am trying to save Honey my pain.
Tom Barnaby
www.intertech-inc.com
Distributed .NET Programming in C# (Apress, June)
-
Re: GetHashCode
Yup. That's why i said *should* and not *must* 
A good hash code generation algorithm will produce as unique a number as
possible, and for certain search algorithm/structures, as even a distribution
as possible too.
-Rob
"Tom Barnaby" <tbarnaby@intertech-inc.com> wrote:
>
>Sorry Rob - you probably know all this. Its just that in my early days hash
>numbers confused me too because they were explained to me as "unique values"
>and I didn't see how that was possible. I am trying to save Honey my pain.
>
>Tom Barnaby
>www.intertech-inc.com
>Distributed .NET Programming in C# (Apress, June)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks