Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

C#의 대소문자를 구분하지 않는 사전

<시간/>

대소문자를 무시하고 비교하려면 대소문자를 구분하지 않는 사전을 사용하십시오.

사전을 선언하는 동안 다음 속성을 설정하여 대소문자를 구분하지 않는 Dictionary −

StringComparer.OrdinalIgnoreCase

다음과 같이 속성을 추가하십시오 -

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

출력

[cricket, 1]
[football, 2]
1
1