不区分大小写的列表大小写、列表

由网友(写不尽的伤痛)分享简介:我需要区分大小写的列表或集合设置类型(字符串)。什么是创建一个最简单的方法是什么?你可以指定你想获得一个词典的按键比较的类型,但我找不到任何类似的清单。I need a case insensitive list or set type of collection (of strings). What is the...

我需要区分大小写的列表或集合设置类型(字符串)。什么是创建一个最简单的方法是什么?你可以指定你想获得一个词典的按键比较的类型,但我找不到任何类似的清单。

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.

推荐答案

看起来像它可以充分利用KeyedCollection类:

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;
    }
}
阅读全文

相关推荐

最新文章