A string as a Hashtable key

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
 
G

Guest

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

Cor Ligthert [MVP]

Jon,

It seems for me easier to use consequently
..ToLower

I hope that I don't miss something

Cor
 
J

Jon Shemitz

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top