c# weird Dictionary ContainsKey or StringComaprer

It's some kind of weird magic, ContainsKey returns false. I tried to use InvariantCulture comparer with the same result.

GameCommands = new Dictionary<string, GameCommand>(StringComparer.Ordinal)
            {
                {"Start new game with deck", StartGame},
                {"​Tell color", TellColor},
                {"​Tell rank", TellRank},
                {"​Drop card", Drop},
                {"​Play card", Play},
            };
Debug.WriteLine(GameCommands.ContainsKey("Tell color"));

False

I solve it by removing quotes in keys and typing them again. I want to know why this is happening.

Jon Skeet
people
quotationmark

You have zero-width spaces (U+200B) at the start of some of your strings. For example, copy this:

{"​Drop card", Drop},

into the Unicode Explorer here, and you'll see something like this:

Unicode explorer

Now, we don't know where that character came from, but I suspect you were copying and pasting the text from somewhere else.

Note that there's nothing wrong with the behaviour of the dictionary, or of string or anything in .NET here... it's just a problem in your source code. You'd see exactly the same behaviour if you expressed the same string more clearly:

{"\u200BDrop card", Drop},

people

See more on this question at Stackoverflow