csharp 字典 key 不区分大小写

Csharp_dict_ignorecase 参考链接:

https://www.tutorialspoint.com/case-insensitive-dictionary-in-chash?ranMID=49144&ranEAID=mP6UMnc5Ozo&ranSITEID=mP6UMnc5Ozo-0dXMNkfvS087.74Ygvbi7Q


今天遇到一个问题,需要在csharp中使用字典,但是字典的key是不区分大小写的,所以需要自己实现一个不区分大小写的字典,好在 csharp 提供这项功能。

核心代码:

  Dictionary <string, int> dict = new Dictionary <string, int>(StringComparer.OrdinalIgnoreCase);

例子

using System;
using System.Collections.Generic;
public class Program {
   public static void Main() {
      Dictionary <string, int> dict = new Dictionary <string, int>(StringComparer.OrdinalIgnoreCase);
      dict.Add("cricket", 1);
      dict.Add("football", 2);
      foreach (var val in dict) {
         Console.WriteLine(val.ToString());
      }
      // case insensitive dictionary i.e. "cricket" is equal to "CRICKET"
      Console.WriteLine(dict["cricket"]);
      Console.WriteLine(dict["CRICKET"]);
   }
}

输出

1
1
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
吸引力法则,大圣灵、外星人和心灵能量
使用 Hugo 构建
主题 StackJimmy 设计