Search code examples
c#.netstringdictionarycollections

A case-insensitive list


I need a case insensitive list or set type of collection (of strings). What is the easiest way to create one? You can specify the type of comparison you want to get on the keys of a Dictionary, but I can't find anything similar for a List.


Solution

  • Looks like its possible to leverage the KeyedCollection class:

    public class Set<T> : KeyedCollection<T,T>
    {
        public Set()
        {}
    
        public Set(IEqualityComparer<T> comparer) : base(comparer)
        {}
    
        public Set(IEnumerable<T> collection)
        {
            foreach (T elem in collection)
            {
                Add(elem);
            }
        }
    
        protected override T GetKeyForItem(T item)
        {
            return item;
        }
    }