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.ContainsKey("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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With