GetHashCode


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: GetHashCode

  1. #1
    Honey Guest

    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.



  2. #2
    Rob Teixeira Guest

    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.
    >
    >



  3. #3
    Patrice Scribe Guest

    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.
    >
    >




  4. #4
    Tom Barnaby Guest

    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)

  5. #5
    Rob Teixeira Guest

    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center