我有一段代码如下:
IDictionary<string, string> dictionary = new Dictionary<string, string>(){
{ "de-DE", "German"},
{ "en‐GB", "English"},
{ "en‐US", "English"},
{ "es‐ES", "Spanish"},
{ "es‐MX", "Spanish"},
{ "fr‐CA", "French"},
{ "fr‐FR", "French"},
{ "hu‐HU", "Hungarian"},
{ "pl‐PL", "Polish"},
{ "pt‐PT", "Portuguese"},
{ "nl‐NL", "Dutch"},
{ "nb‐NO", "Norwegian"},
{ "sv‐SE", "Swedish"},
{ "it‐IT", "Italian"},
};
bool check = dictionary.ContainsKey("en-US");
Console.WriteLine(check);
check
返回false
。
有人可以帮忙解释一下吗?
答案 0 :(得分:5)
你的破折号不一样。在Notepad++中查看文本的样子(当您需要调试与此类相关的字符时很有用):
将短划线替换为实际的-
,the code works as expected。
修改强>
谢谢,@斯科特张伯伦为。net fiddle that highlights the character differences。
答案 1 :(得分:1)
正如已经指出的你的破折号不是同一个。我已向您的Dictionary
添加了另一项项目,该项目可为您提供预期结果,并通过IL比较基础Ildasm
以了解其中的差异:
{ "en‐US", "English"},
{ "en-US", "English2"},
和
IL_002d: nop
IL_002e: dup
IL_002f: ldstr bytearray (65 00 6E 00 10 20 55 00 53 00 ) // e.n.. U.S.
IL_0034: ldstr "English"
IL_0039: callvirt instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,string>::Add(!0,
!1)
IL_003e: nop
IL_003f: dup
IL_0040: ldstr "en-US"
IL_0045: ldstr "English2"
IL_004a: callvirt instance void class [mscorlib]System.Collections.Generic.Dictionary`2<string,string>::Add(!0,
!1)