2

这是某种奇怪的魔法, ContainsKey 返回 false。我尝试使用具有相同结果的 InvariantCulture 比较器。

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"));

错误的

我通过删除键中的引号并再次键入它们来解决它。我想知道为什么会这样。

4

1 回答 1

3

在某些字符串的开头有零宽度空格 (U+200B)。例如,复制以下内容:

{"​Drop card", Drop},

在这里进入Unicode Explorer,你会看到这样的东西:

Unicode 浏览器

现在,我们不知道那个字符来自哪里,但我怀疑你是从其他地方复制和粘贴文本。

请注意,字典的行为string或.NET 中的任何内容都没有问题……这只是源代码中的问题。如果您更清楚地表达相同的字符串,您会看到完全相同的行为:

{"\u200BDrop card", Drop},
于 2016-03-24T16:32:33.993 回答