C#中具有字符串键类型的不区分大小写的字典
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13988643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Case-INsensitive Dictionary with string key-type in C#
提问by Mr. Boy
If I have a Dictionary<String,...>
is it possible to make methods like ContainsKey
case-insensitive?
如果我有 aDictionary<String,...>
是否可以制作ContainsKey
不区分大小写的方法?
This seemed related, but I didn't understand it properly: c# Dictionary: making the Key case-insensitive through declarations
这似乎相关,但我没有正确理解它:c# Dictionary:通过声明使 Key 不区分大小写
采纳答案by Konrad Rudolph
This seemed related, but I didn't understand it properly: c# Dictionary: making the Key case-insensitive through declarations
这似乎相关,但我没有正确理解它:c# Dictionary:通过声明使 Key 不区分大小写
It is indeed related. The solution is to tell the dictionary instance not to use the standard string compare method (which is case sensitive) but rather to use a case insensitive one. This is done using the appropriate constructor:
确实是有关系的。解决方案是告诉字典实例不要使用标准字符串比较方法(区分大小写),而是使用不区分大小写的方法。这是使用适当的构造函数完成的:
var dict = new Dictionary<string, YourClass>(
StringComparer.InvariantCultureIgnoreCase);
The constructor expects an IEqualityComparer
which tells the dictionary how to compare keys.
构造函数需要一个IEqualityComparer
告诉字典如何比较键的参数。
StringComparer.InvariantCultureIgnoreCase
gives you an IEqualityComparer
instance which compares strings in a case-insensitive manner.
StringComparer.InvariantCultureIgnoreCase
为您提供一个IEqualityComparer
以不区分大小写的方式比较字符串的实例。
回答by Steve
var myDic = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
myDic.Add("HeLlo", "hi");
if (myDic.ContainsKey("hello"))
Console.WriteLine(myDic["hello"]);
回答by Kurkula
There are few chances where your deal with dictionary which is pulled from 3rd party or external dll. Using linq
您处理从 3rd 方或外部 dll 中提取的字典的可能性很小。使用 linq
YourDictionary.Any(i => i.KeyName.ToLower().Contains("yourstring")))
YourDictionary.Any(i => i.KeyName.ToLower().Contains("yourstring")))
回答by Ferry Jongmans
I just ran into the same kind of trouble where I needed a caseINsensitive dictionary in a ASP.NET Core controller.
我刚刚遇到了同样的问题,我需要在 ASP.NET Core 控制器中使用不区分大小写的字典。
I wrote an extension method which does the trick. Maybe this can be helpful for others as well...
我写了一个扩展方法来解决这个问题。也许这对其他人也有帮助......
public static IDictionary<string, TValue> ConvertToCaseInSensitive<TValue>(this IDictionary<string, TValue> dictionary)
{
var resultDictionary = new Dictionary<string, TValue>(StringComparer.InvariantCultureIgnoreCase);
foreach (var (key, value) in dictionary)
{
resultDictionary.Add(key, value);
}
dictionary = resultDictionary;
return dictionary;
}
To use the extension method:
要使用扩展方法:
myDictionary.ConvertToCaseInSensitive();
Then get a value from the dictionary with:
然后从字典中获取一个值:
myDictionary.ContainsKey("TheKeyWhichIsNotCaseSensitiveAnymore!");