473,404 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

CaseInsensitiveComparer with Generic List problem

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
2 3380
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Paul E Collins | last post by:
Shouldn't the CaseInsensitiveComparer sort case-insensitively? The following code prints out 'AZaz', which indicates a case-sensitive sort. What do I need to do to perform a case-insensitive sort...
5
by: majm | last post by:
I'm trying to implement strongly typed lists in the 2.0 framework. I'm using VS2005 beta 2. So far, System.Collections.Generic.List appears to be the ideal solution. However, the...
10
by: steve bull | last post by:
I have a class SwatchPanel which takes Swatch as a parameter type. How can I call a static function within the Swatch class? For example the code below fails on TSwatch.Exists. How can I get the...
2
by: Nicolas Fleury | last post by:
Hi, I have a field named "BaseClasses" in C++ as in the following simplified code: using Collections::Generic::List; namespace A2M { namespace LipIntrospection { public ref class...
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
0
by: crazyone | last post by:
I've got a gaming framework i'm building and i want to save myself the trouble of reading and writting the complete game data to a custom file and load/save it to an XML file but i'm getting...
3
by: Seth Gecko | last post by:
Hi I am working with generic lists of various objects and a control dealing with these lists. For instance: A parent form holds: dim Walls as List(Of wall) dim Segments as List(Of segment) ...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
11
by: Scott Stark | last post by:
Hello, The code below represents a singly-linked list that accepts any type of object. You can see I'm represting the Data variable a System.Object. How would I update this code to use...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.