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

C#에서 지정된 초기 크기로 대소문자 구분 HybridDictionary 만들기

<시간/>

지정된 초기 크기로 대소문자를 구분하는 HybridDictionary를 생성하기 위한 코드는 다음과 같습니다 -

예시

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main(){
      HybridDictionary myDict = new HybridDictionary(5, false);
      myDict.Add("A", "AB");
      myDict.Add("B", "BC");
      myDict.Add("C", "DE");
      myDict.Add("D", "FG");
      myDict.Add("e", "fg");
      Console.WriteLine("Key/Value pairs...");
      foreach(DictionaryEntry de in myDict)
      Console.WriteLine("Key = "+de.Key + ", Value = " + de.Value);
   }
}

출력

이것은 다음과 같은 출력을 생성합니다 -

Key/Value pairs...
Key = A, Value = AB
Key = B, Value = BC
Key = C, Value = DE
Key = D, Value = FG
Key = e, Value = fg

예시

이제 다른 예를 살펴보겠습니다 -

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main(){
      HybridDictionary myDict = new HybridDictionary(5, true);
      myDict.Add("A", "AB");
      myDict.Add("B", "BC");
      myDict.Add("C", "DE");
      myDict.Add("e", "fg");
      myDict.Add("E", "FG");
      Console.WriteLine("key/Value pairs...");
      foreach(DictionaryEntry de in myDict)
      Console.WriteLine("Key = "+de.Key + ", Value = " + de.Value);
   }
}

출력

이것은 다음 오류를 생성합니다 -

Run-time exception : An entry with the same key already exists.
Stack Trace:
[System.ArgumentException: An entry with the same key already exists.]
   at System.Collections.Specialized.ListDictionary.Add(Object key, Object value)
   at System.Collections.Specialized.HybridDictionary.Add(Object key, Object value)
   at Demo.Main() :line 15