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

C#의 StringDictionary 클래스?

<시간/>

StringDictionay 클래스는 키와 값이 개체가 아닌 문자열로 강력하게 형식화된 해시 테이블을 구현합니다.

다음은 StringDictionary 클래스의 속성입니다 -

Sr.No 속성 및 설명
1 카운트
StringDictionary의 키/값 쌍 수를 가져옵니다.
2 동기화됨
StringDictionary에 대한 액세스가 동기화되었는지(스레드 안전) 여부를 나타내는 값을 가져옵니다.
3 항목[문자열]
지정된 키와 연결된 값을 가져오거나 설정합니다.
4
StringDictionary에서 키 컬렉션을 가져옵니다.
5 SyncRoot
StringDictionary에 대한 액세스를 동기화하는 데 사용할 수 있는 개체를 가져옵니다.
6
StringDictionary의 값 컬렉션을 가져옵니다.

다음은 StringDictionary 클래스의 메소드 중 일부입니다 -

Sr.No 방법 및 설명
1 추가(문자열, 문자열)
지정된 키와 값이 있는 항목을 StringDictionary에 추가합니다.
2 지우기()
StringDictionary에서 모든 항목을 제거합니다.
3 ContainsKey(문자열)
StringDictionary에 특정
4 값(문자열) 포함
StringDictionary에 특정 값이 포함되어 있는지 확인합니다.
5 CopyTo(배열, Int32)
문자열 사전 값을 지정된 인덱스의 1차원 Array 인스턴스에 복사합니다.
6 같음(객체)
지정된 개체가 현재 개체와 같은지 여부를 확인합니다. (Object에서 상속됨)

예시

이제 몇 가지 예를 살펴보겠습니다 -

두 StringDictionary 객체가 같은지 확인하려면 코드는 다음과 같습니다. -

using System;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      StringDictionary strDict1 = new StringDictionary();
      strDict1.Add("A", "John");
      strDict1.Add("B", "Andy");
      strDict1.Add("C", "Tim");
      strDict1.Add("D", "Ryan");
      strDict1.Add("E", "Kevin");
      strDict1.Add("F", "Katie");
      strDict1.Add("G", "Brad");
      StringDictionary strDict2 = new StringDictionary();
      strDict2.Add("A", "John");
      strDict2.Add("B", "Andy");
      strDict2.Add("C", "Tim");
      strDict2.Add("D", "Ryan");
      strDict2.Add("E", "Kevin");
      strDict2.Add("F", "Katie");
      strDict2.Add("G", "Brad");
      StringDictionary strDict3 = new StringDictionary();
      strDict3 = strDict2;
      Console.WriteLine("Is Dictionary2 equal to Dictionary3? = "+strDict2.Equals(strDict3));
      Console.WriteLine("Is Dictionary1 equal to Dictionary3? = "+strDict1.Equals(strDict3));
   }
}

출력

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

Is Dictionary2 equal to Dictionary3? = True
Is Dictionary1 equal to Dictionary3? = False

예시

StringDictionary가 동기화되었는지 확인하는 코드는 다음과 같습니다 -

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      StringDictionary strDict1 = new StringDictionary();
      strDict1.Add("A", "John");
      strDict1.Add("B", "Andy");
      strDict1.Add("C", "Tim");
      strDict1.Add("D", "Ryan");
      strDict1.Add("E", "Kevin");
      strDict1.Add("F", "Katie");
      strDict1.Add("G", "Brad");
      Console.WriteLine("StringDictionary1 elements...");
      foreach(DictionaryEntry de in strDict1) {
         Console.WriteLine(de.Key + " " + de.Value);
      }
      StringDictionary strDict2 = new StringDictionary();
      strDict2.Add("1", "A");
      strDict2.Add("2", "B");
      strDict2.Add("3", "C");
      strDict2.Add("4", "D");
      strDict2.Add("5", "E");
      Console.WriteLine("\nStringDictionary2 key-value pairs...");
      IEnumerator demoEnum = strDict2.GetEnumerator();
      DictionaryEntry d;
      while (demoEnum.MoveNext()) {
         d = (DictionaryEntry)demoEnum.Current;
         Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
      }
      Console.WriteLine("Is the StringDictionary2 synchronized? = "+strDict2.IsSynchronized);
   }
}

출력

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

StringDictionary1 elements...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
StringDictionary2 key-value pairs...
Key = 1, Value = A
Key = 2, Value = B
Key = 3, Value = C
Key = 4, Value = D
Key = 5, Value = E
Is the StringDictionary2 synchronized? = False