我花了几个小时试图弄清楚为什么我的通用 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"))
控制台每次都显示“False”。它应该显示“真实”。
如果我使用:
Console.WriteLine(test."frog"))
我得到一个 KeyNotFoundException。
似乎完全忽略了比较器参数。
到底是怎么回事?
最佳答案
正如所暗示的 here ,这是一个简单的拼写错误。
问题是系统。 字符串比较 .OrdinalIgnoreCase 是一个整数枚举。
应该是系统。 StringComparer .OrdinalIgnoreCase
New System.Collections.Generic.Dictionary(Of String, String)(System.StringComparison.OrdinalIgnoreCase) 实际上是在调用新建(容量为整数)重载构造函数,并传递 5。
因此,要使其按预期工作,实例化行应为:
Dim test As New System.Collections.Generic.Dictionary(Of String, String)(System.StringComparer.OrdinalIgnoreCase)
关于vb.net - 不区分大小写的字典不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6352364/