Problem with HashTable
-
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..
-
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..
-
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.
-
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.
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.
-
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.
-
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.
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.