Requires: .Net collection that stores a bunch of case-insensitive strings quickly and efficiently

I am looking for a simple collection that will store a bunch of strings in case insensitive . I need at least the Contains() and Remove() method to see if a specific row exists and delete that row.

I tried List<string> , but it is case sensitive. I needed to use a case insensitive Dictionary<TKey, T> , but it "feels" like a waste of space. Running ToLower() for each row is a waste of performance.

Does anyone know which .Net collection I should use?

+6
collections generics c #
source share
5 answers

You should use new HashSet<string>(StringComparer.OrdinalIgnoreCase) .
Please note that this is an unordered set.

+18
source share

You can use StringDictionary .

+2
source share

If the same problem was solved today. If you can include Linq, then your List overloads methods with comparison.

 using System.Linq; List<string> stringList = new List<string>(); stringList.Contains("hello", StringComparer.OrdinalIgnoreCase); 

Hope this helps: Martin

0
source share

By default, the Dictionary is not case sensitive. But you can implement your own option to make it sensitive. (Maybe I'm wrong: D)

I had the same problem with Dictionary, but after trying many implementations of IEquality, finally I settled the score in LINQ.

 string k = customers.Where(c => c.Key.Equals(valueToSearch, StringComparison.OrdinalIgnoreCase)).FirstOrDefault().Key; if (!string.IsNullOrEmpty(k) && k.ToUpper() == valueToSearch.ToUpper()) { // Do some thing } 

Hope this helps someone in the future.

Sanjay Zalke

0
source share

Write your own Contains() and Remove() methods that perform case-insensitive comparisons.

-one
source share

All Articles