简体   繁体   English

如何使字典不区分大小写

[英]How can I make dictionary case insensitive

I am trying to make Dictionary case insensitive. 我试图使Dictionary不区分大小写。 But, I declare it as a property, how can I make that insensitive. 但是,我将其声明为属性,如何使其不敏感。

I know that while defining, I can use it like : 我知道在定义时,我可以使用它:

var dict = new Dictionary<string, YourClass>(
        StringComparer.InvariantCultureIgnoreCase);

But, I am defining it in my interface and class respectively like 但是,我在我的界面和类中分别定义它

IDictionary<string, string> dict { get; }
public Dictionary<string, string> dict { get; set; }

How can I make this case insensitive ? 如何使这个案例不敏感?

You mentioned that You define it in Your class like: 你提到你在你的类中定义它:

public Dictionary<string, string> dict { get; set; }

So, instead of using short form for auto properties, use the full form: 因此,使用完整形式而不是使用简短形式的自动属性:

Dictionary<string, string> _dict = new Dictionary<string, string>(
    StringComparer.InvariantCultureIgnoreCase);
public Dictionary<string, string> dict
{
    get { return _dict; }
    set { _dict = value; }
}

If You are using C# 6.0, You could also probably even write it using the new auto property initializers syntax: 如果您使用的是C#6.0,您甚至可以使用新的auto属性初始化器语法编写它:

public Dictionary<string, string> dict { get; set; } = new Dictionary<string, string>(
    StringComparer.InvariantCultureIgnoreCase);

Links: 链接:

The only way you could enforce it on the class or interface level is you make a new derived type and use that type. 在类或接口级别强制执行它的唯一方法是创建一个新的派生类型并使用该类型。

public class CaseInsensitiveDictionary<TValue> : Dictionary<string, TValue>
{
    public CaseInsensitiveDictionary() : base(StringComparer.InvariantCultureIgnoreCase)
    {
    }
}

Then in your interface you would do 然后在你的界面中你会做

CaseInsensitiveDictionary<YourClass> { get; }

You can't. 你不能。 The comparer is a read-only property ( https://msdn.microsoft.com/en-us/library/ms132092(v=vs.110).aspx ). 比较器是只读属性( https://msdn.microsoft.com/en-us/library/ms132092(v=vs.110).aspx )。 Declare your comparer during the construction of your object. 在构造对象期间声明比较器。

Allowing one to change comparers at runtime would be seriously problematic as it could result in sudden key collisions that would require the dictionary to be restructured. 允许一个人在运行时更改比较器将是严重的问题,因为它可能导致突然的密钥冲突,这将需要重组字典。

If the case insensitivity is intended to be part of the contract, it sounds like what you really want to be doing is encapsulating this logic inside something else and then exposing that on your interface instead. 如果不区分大小写是合同的一部分,那么您真正想要做的就是将此逻辑封装在其他内容中,然后在您的界面上公开它。

Depending on what specific functionality you're trying to expose, something as simple as this might do the trick: 根据您尝试公开的具体功能,可以使用简单的方法:

public class CaseInsensitiveIndex
{
    private readonly Dictionary<string, object> _collection = 
        new Dictionary<string, object>();

    public object this[string index]
    {
        get { return _collection[index.ToLower()]; }
        set { _collection[index.ToLower()] = value; }
    }
}

public interface IHasCaseInsensitiveIndex
{
    CaseInsensitiveIndex Index { get; }
}

It seems a little odd to me to be publicly exposing a collection via an interface like this (this feels like it should be an internal implementation detail... but that depends on the context - consider whether your encapsulation needs work) but this is the closest thing to what you're trying to achieve that I can think of. 通过这样的界面公开展示一个集合似乎有点奇怪(这感觉它应该是一个内部实现细节......但这取决于上下文 - 考虑你的封装是否需要工作)但这是我能想到的最接近你想要实现的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM