Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem with HashTable

Problem with HashTable

Scheduled Pinned Locked Moved C#
questiondatabasedata-structurescryptographyhelp
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NarVish
    wrote on last edited by
    #1

    Hi, I created a hash table with 3 elements. hashtbl.Add(1, "aaa"); hashtbl.Add(2, "bbb"); hashtbl.Add(3, "ccc"); When I'm trying to modify the hash table as following, hashtbl[2] ="xxx"; hashtable's length increased by 1. After the above statement, hashtbl looks as following. hashtbl[2, "xxx"] hashtbl[1, "aaa"] hashtbl[2, "bbb"] hashtbl[3, "ccc"] How can I restrict the hash table to add a new element with the same index, 2? I would like to get hashtable as below after the modification. hashtbl[1, "aaa"] hashtbl[2, "xxx"] hashtbl[3, "ccc"] Thanks in advance..

    V 1 Reply Last reply
    0
    • N NarVish

      Hi, I created a hash table with 3 elements. hashtbl.Add(1, "aaa"); hashtbl.Add(2, "bbb"); hashtbl.Add(3, "ccc"); When I'm trying to modify the hash table as following, hashtbl[2] ="xxx"; hashtable's length increased by 1. After the above statement, hashtbl looks as following. hashtbl[2, "xxx"] hashtbl[1, "aaa"] hashtbl[2, "bbb"] hashtbl[3, "ccc"] How can I restrict the hash table to add a new element with the same index, 2? I would like to get hashtable as below after the modification. hashtbl[1, "aaa"] hashtbl[2, "xxx"] hashtbl[3, "ccc"] Thanks in advance..

      V Offline
      V Offline
      Vega02
      wrote on last edited by
      #2

      Is this using the built-in HashTable class? I would recommend using Dictionary<int, string>[^] instead. Your problem may have been due to some weirdness while boxing.

      N G 2 Replies Last reply
      0
      • V Vega02

        Is this using the built-in HashTable class? I would recommend using Dictionary<int, string>[^] instead. Your problem may have been due to some weirdness while boxing.

        N Offline
        N Offline
        NarVish
        wrote on last edited by
        #3

        Thank you Vega02. Its worked and come to know about Dictionary class.

        1 Reply Last reply
        0
        • V Vega02

          Is this using the built-in HashTable class? I would recommend using Dictionary<int, string>[^] instead. Your problem may have been due to some weirdness while boxing.

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          Vega02 wrote:

          Your problem may have been due to some weirdness while boxing.

          That is exactly the reason for the problem. The HashTable class uses reference types as identifiers, so if you try to use value types as identifiers, they are boxed first. Each time you box the value 2, it becomes a new object on the heap, containing the value. As they are separate objects, they have different hash codes.

          Despite everything, the person most likely to be fooling you next is yourself.

          V 1 Reply Last reply
          0
          • G Guffa

            Vega02 wrote:

            Your problem may have been due to some weirdness while boxing.

            That is exactly the reason for the problem. The HashTable class uses reference types as identifiers, so if you try to use value types as identifiers, they are boxed first. Each time you box the value 2, it becomes a new object on the heap, containing the value. As they are separate objects, they have different hash codes.

            Despite everything, the person most likely to be fooling you next is yourself.

            V Offline
            V Offline
            Vega02
            wrote on last edited by
            #5

            Shouldn't the hash codes be the same, since they represent the same underlying value? It doesn't matter that they're boxed value types. They don't have reference equality, but they still have value equality, hence they should have the same hash code.

            G 1 Reply Last reply
            0
            • V Vega02

              Shouldn't the hash codes be the same, since they represent the same underlying value? It doesn't matter that they're boxed value types. They don't have reference equality, but they still have value equality, hence they should have the same hash code.

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              Vega02 wrote:

              Shouldn't the hash codes be the same, since they represent the same underlying value?

              No, the values are boxed in a type Object, and the hash code of the Object is not based on the boxed value. The GetHashCode method of the Object type is not suitable for use as key in a hash table. From the documentation: "the default implementation of this method must not be used as a unique object identifier for hashing purposes" MSDN Library: Object.GetHashCode method[^]

              Despite everything, the person most likely to be fooling you next is yourself.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups