gpt4 book ai didi

c# weird Dictionary ContainsKey or StringComaprer 收藏

转载 作者:行者123 更新时间:2023-11-30 21:50:39 24 4
gpt4 key购买 nike

这是某种奇怪的魔法,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"));

错误

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

最佳答案

您的某些字符串的开头有零宽度空格 (U+200B)。例如,复制这个:

{"​Drop card", Drop},

进入Unicode Explorer here ,你会看到类似这样的东西:

Unicode explorer

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

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

{"\u200BDrop card", Drop},

关于c# weird Dictionary ContainsKey or StringComaprer 收藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36205201/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com