Hashtable key type
-
Does a C# hashtable key have to be a string or could it be an int? Cheers, Bruce :confused:
-
Does a C# hashtable key have to be a string or could it be an int? Cheers, Bruce :confused:
Hi Bruce, a Hashtable, and since 2.0 also a generic Dictionary, does not impose limitations on the type of keys and values it takes. The key can be a string, an int, a MenuItem, whatever. It must have a decent hashing function, as documented for Dictionary< TKey, TValue>: As long as an object is used as a key in the Dictionary< (Of < (TKey, TValue> )> ), it must not change in any way that affects its hash value. Every key in a Dictionary< (Of < (TKey, TValue> )> ) must be unique according to the dictionary's equality comparer. A key cannot be nullNothingnullptra null reference (Nothing in Visual Basic), but a value can be, if the value type TValue is a reference type. :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
Does a C# hashtable key have to be a string or could it be an int? Cheers, Bruce :confused:
It can be anything.
-
It can be anything.
So if I make it an int for example do I have to overide any hashing functions? In my ap the key would be a uint32 and the value an int. Cheers, Bruce :thumbsup:
-
So if I make it an int for example do I have to overide any hashing functions? In my ap the key would be a uint32 and the value an int. Cheers, Bruce :thumbsup:
Bruce Coward wrote:
So if I make it an int for example do I have to overide any hashing functions?
No, you don't have to. You could also consider using a generic dictionary instead of HashTable.
Navaneeth How to use google | Ask smart questions