Case-insensitive List<string>

M

Mark Rae

Hi,

Is it possible to create a case-insensitive List<string> collection?

E.g.

List<string> MyList = new List<string>;
MyList.Add("MyString");

So that:

MyList.Contains("mystring")
MyList.Contains("MyString")
MyList.Contains("MYSTRING")

all return true.

Google shows plenty of examples of how to do this with Dictionary<string,
string> collections e.g.

Dictionary<string, string> MyDictionary = new Dictionary<string,
string>(StringComparer.CurrentCultureIgnoreCase);

but I can't find an equivalent for List<string>...

Any assistance gratefully received.

Mark
 
N

Nicholas Paldino [.NET/C# MVP]

Mark,

No, the List does not support any other comparison for Contains. You
will have to use the dictionary method to determine if you have a
case-insensitive string in your set.

Hope this helps.
 
A

Andreas Mueller

Mark said:
Hi,

Is it possible to create a case-insensitive List<string> collection?

E.g.

List<string> MyList = new List<string>;
MyList.Add("MyString");

So that:

MyList.Contains("mystring")
MyList.Contains("MyString")
MyList.Contains("MYSTRING")

all return true.

Google shows plenty of examples of how to do this with Dictionary<string,
string> collections e.g.

Dictionary<string, string> MyDictionary = new Dictionary<string,
string>(StringComparer.CurrentCultureIgnoreCase);

but I can't find an equivalent for List<string>...

Any assistance gratefully received.

Mark
No, but you can get the information you need from a regular List<string>:

List<string> s = new List<string>();
bool contains = null != s.Find(delegate(string str)
{
return str.ToLower().Equals("MyString".ToLower());
});


HTH,
Andy
 

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