A string as a Hashtable key

  • Thread starter Thread starter thechaosengine
  • Start date Start date
T

thechaosengine

Hi all,

Is it ok to use a string as the key for Hashtable entries? I want to use
the name of entity in question, which I know will always be unique.
Do I have to do anything fancy equality-wise or are there any caveats I should
be aware of?

Thanks to anyone who can advise.

Kindest Regards

tce
 
Is it ok to use a string as the key for Hashtable entries?

yes, it is ok to use strings, i do it all the time. the comparison is case
sensitive, so if you want to have case insensitive keys, then drive your key
strings to upper case (or lower if you prefer).
 
Jon,

It seems for me easier to use consequently
..ToLower

I hope that I don't miss something

Cor
 
Cor Ligthert said:
It seems for me easier to use consequently
.ToLower

I hope that I don't miss something

Easier, maybe, but not always desirable. Using a case-insensitive
comparison function allows semantics like file names: Case doesn't
matter, but is preserved.

If I use "Jon Shemitz" as a key, I might prefer to get back "Jon
Shemitz" when I enumerate the Keys, not "jon shemitz" or "JON
SHEMITZ".
 
Cor Ligthert said:
I don't know what program language you use, however in VBNet is for that as
well a function.

http://msdn.microsoft.com/library/d...ry/en-us/vblr7/html/vamscstrconvconstants.asp

It is usable in C# as well of course by setting a reference to the right
namespace.

I'm not sure how that addresses the issue of preserving case, if that
is desirable. If the key is "Rip van Winkle", then proper-casing it as
"Rip Van Winkle" is just as wrong as "rip van winkle" or "RIP VAN
WINKLE".
 
Back
Top