Creating a case-insensitive Hashtable in v2

M

Mark Rae

Hi,

In v1.x, I used to use the following code to create a case-insensitive
Hashtable:

Hashtable MyHashtable = new
Hashtable(CaseInsensitiveHashCodeProvider.Default,
CaseInsensitiveComparer.Default);

In v2, this generates the following warnings:

Warning 1 'System.Collections.CaseInsensitiveHashCodeProvider' is obsolete:
'Please use StringComparer instead.
Warning 4
'System.Collections.Hashtable.Hashtable(System.Collections.IHashCodeProvider,
System.Collections.IComparer)' is obsolete: 'Please use
Hashtable(IEqualityComparer) instead.

However, when I go to the online Help for Hashtable, that's pretty much the
code it still suggests.

Can anyone please tell me the v2 way of creating a case-insensitive
Hashtable?

Any assistance gratefully received.

Mark
 
J

Jon Skeet [C# MVP]

Mark Rae said:
In v1.x, I used to use the following code to create a case-insensitive
Hashtable:

Hashtable MyHashtable = new
Hashtable(CaseInsensitiveHashCodeProvider.Default,
CaseInsensitiveComparer.Default);

In v2, this generates the following warnings:

Warning 1 'System.Collections.CaseInsensitiveHashCodeProvider' is obsolete:
'Please use StringComparer instead.
Warning 4
'System.Collections.Hashtable.Hashtable(System.Collections.IHashCodeProvider,
System.Collections.IComparer)' is obsolete: 'Please use
Hashtable(IEqualityComparer) instead.

However, when I go to the online Help for Hashtable, that's pretty much the
code it still suggests.

Can anyone please tell me the v2 way of creating a case-insensitive
Hashtable?

Well, the preferred way of creating a map in 2.0 is usually to use
Dictionary<TKey, TValue>, passing in an IEqualityComparer<T>. In this
case, you would create a Dictionary<string,Foo> (where Foo is whatever
type your values are) and pass in an instance of StringComparer - eg
StringComparer.OrdinalIgnoreCase or
StringComparer.CurrentCultureIgnoreCase.
 
M

Mark Rae

A map from keys to values.

Oh right, I see now...
Not most of the time. Think of Dictionary<TKey, TValue> as a strongly-
typed version of Hashtable.

OK, thanks for that. I'm still getting to grips with v2, so that sort of
advice is particularly helpful.
 

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