By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,565 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,565 IT Pros & Developers. It's quick & easy.

CaseInsensitiveComparer with Generic List problem

P: n/a
Hi

I need to do a case insensitive search on a Generic list. I'm trying to use
the BinarySearch method but I get a complier error "cannot convert from
'System.Collections.CaseInsensitiveComparer' to
'System.Collections.Generic.IComparer<string>' "

List<stringlist = new List<string>();
list.Add(str1);
list.Add(str2);
list.Add(str3);
int index = list.BinarySearch(otherstr, new CaseInsensitiveComparer());

What's the correct procedure?

Thanks
Andrew
Jul 10 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
try: int index = list.BinarySearch(otherstr,
StringComparer.OrdinalIgnoreCase);

or InvariantCultureIgnoreCase or CurrentCultureIgnoreCase. Note that
binary search only works on sorted data; depending on what you are
doing, a SortedList<TKey,TValuemight also be worth considering,
which has an IndexOfKey method.

Marc
Jul 10 '08 #2

P: n/a
Andrew Jocelyn <j0**@newsgroups.nospamwrote:
I need to do a case insensitive search on a Generic list. I'm trying to use
the BinarySearch method but I get a complier error "cannot convert from
'System.Collections.CaseInsensitiveComparer' to
'System.Collections.Generic.IComparer<string>' "

List<stringlist = new List<string>();
list.Add(str1);
list.Add(str2);
list.Add(str3);
int index = list.BinarySearch(otherstr, new CaseInsensitiveComparer());

What's the correct procedure?
Use StringComparer.*IgnoreCase (where * is Ordinal, CurrentCulture or
InvariantCulture depending on your needs).

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 10 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.