不区分大小写的列表 [英] A case-insensitive list

查看:206
本文介绍了不区分大小写的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
    }
}

这篇关于不区分大小写的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆