相关疑难解决方法(0)

使用哪个版本的StringComparer

如果我想要一个不区分大小写的字符串键入字典,我应该使用哪个版本的StringComparer给定这些约束:

  • 字典中的键来自C#代码或仅用英语语言环境编写的配置文件(美国或英国)
  • 该软件已国际化,将在不同的区域运行

我通常使用StringComparer.InvariantCultureIgnoreCase但不确定这是否是正确的情况.这是示例代码:

Dictionary< string, object> stuff = new Dictionary< string, object>(StringComparer.InvariantCultureIgnoreCase);
Run Code Online (Sandbox Code Playgroud)

.net c# string internationalization

20
推荐指数
2
解决办法
7970
查看次数

Case Insensitive Dictionary不起作用

我花了几个小时试图弄清楚为什么我的泛型字典(Of String,String)不会忽略大小写.

这是我的代码:

Dim test As New System.Collections.Generic.Dictionary(Of String, String)(System.StringComparison.OrdinalIgnoreCase)
test.Add("FROG", "1")
Console.WriteLine(test.ContainsKey("frog"))
Run Code Online (Sandbox Code Playgroud)

控制台每次都显示"False".它应该显示"真实".

如果我使用:

Console.WriteLine(test."frog")) 
Run Code Online (Sandbox Code Playgroud)

我得到一个KeyNotFoundException.

看起来似乎完全忽略了Comparer参数.

到底是怎么回事?

vb.net generics dictionary case-insensitive

7
推荐指数
1
解决办法
6827
查看次数