繁体   English   中英

Dictionary.ContainsKey StringComparer.Ordinal

[英]Dictionary.ContainsKey StringComparer.Ordinal

我正在使用带有.NET 3.5的C#中的Dictionary。 我创建了一个Dictionary <string, int >对象,并将其传递给StringComparer.Ordinal相等比较器。 但是,当我执行以下代码时,我没有得到期望的结果:

Dictionary<string, int> theDictionary = new Dictionary<string, int>(StringComparer.Ordinal);
theDictionary.Add("First", 1);
bool exists = theDictionary.ContainsKey("FIRST");    // equals true, when it should not

我在这里没看到什么?

您确定没有使用StringComparer.OrdinalIgnoreCase吗?

这段代码使用C#v3.5编译器为我输出false:

using System;
using System.Collections.Generic;

    static class Program
    {
      static void Main()
      {
        Dictionary<string, int> theDictionary = new Dictionary<string, int>(StringComparer.Ordinal);
        theDictionary.Add("First", 1);
        bool exists = theDictionary.ContainsKey("FIRST");

        Console.WriteLine(exists);
      }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM