开发者

Case Insensitive Dictionary not working

I have spend a couple of hours trying to figure out why my generic Dictionary(Of String, String) is not ignoring case.

Here is my code:

Dim test As New System.Collections.Generic.Dictionary(Of String, String)(System.StringComparison.OrdinalIgnoreCase)
test.Add("FROG", "1")
Console.WriteLine(test.C开发者_StackOverflowontainsKey("frog"))

The console shows "False" every time. It should be showing "True".

If I use:

Console.WriteLine(test."frog")) 

I get a KeyNotFoundException.

It seems as if the Comparer parameter is being completely ignored.

What is going on?


As hinted here, it's a simple spelling mistake.

The issue is System.StringComparison.OrdinalIgnoreCase is an Integer Enum.
It should be System.StringComparer.OrdinalIgnoreCase

New System.Collections.Generic.Dictionary(Of String, String)(System.StringComparison.OrdinalIgnoreCase) is actually calling the New(capacity As Integer) overloaded constructor, and passing 5.

So, to make it all work as expected, the instantiation line should read:

Dim test As New System.Collections.Generic.Dictionary(Of String, String)(System.StringComparer.OrdinalIgnoreCase)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜