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

C#의 ListDictionary 클래스

<시간/>

ListDictionary 클래스는 단일 연결 목록을 사용하여 IDictionary를 구현합니다. 일반적으로 10개 미만의 항목을 포함하는 컬렉션에 권장됩니다.

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

Sr.No 속성 및 설명
1 카운트
ListDictionary에 포함된 키/값 쌍의 수를 가져옵니다.
2 IsFixedSize
ListDictionary의 크기가 고정되어 있는지 여부를 나타내는 값을 가져옵니다.
3 읽기 전용
ListDictionary가 읽기 전용인지 여부를 나타내는 값을 가져옵니다.
4 동기화됨
ListDictionary가 동기화되었는지(스레드 안전) 여부를 나타내는 값을 가져옵니다.
5 항목[객체]
지정된 값과 연결된 값을 가져오거나 설정합니다.
6
ListDictionary의 키를 포함하는 ICollection을 가져옵니다.
7 SyncRoot
ListDictionary에 대한 액세스를 동기화하는 데 사용할 수 있는 개체를 가져옵니다.
8
ListDictionary의 값을 포함하는 ICollection을 가져옵니다.

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

Sr.No 방법 및 설명
1 추가(객체, 객체)
지정된 키와 값이 있는 항목을 ListDictionary에 추가합니다.
2 지우기()
ListDictionary에서 모든 항목을 제거합니다.
3 포함(객체)
ListDictionary에 특정 키가 포함되어 있는지 여부를 결정합니다.
4 CopyTo(배열, Int32)
ListDictionary 항목을 지정된 인덱스의 1차원 Array 인스턴스에 복사합니다.
5 같음(객체)
지정된 개체가 현재 개체와 같은지 여부를 확인합니다. (다음에서 상속됨 ​​Object)
6 GetEnumerator()
ListDictionary를 반복하는 IDictionaryEnumerator를 반환합니다.
7 GetHashCode()
기본 해시 함수 역할을 합니다. (다음에서 상속됨 ​​Object)
8 GetType()
현재 인스턴스의 유형을 가져옵니다. (다음에서 상속됨 ​​Object)

예시

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

ListDictionary가 읽기 전용인지 확인하려면 코드는 다음과 같습니다. -

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      ListDictionary dict1 = new ListDictionary();
      dict1.Add("A", "Books");
      dict1.Add("B", "Electronics");
      dict1.Add("C", "Smart Wearables");
      dict1.Add("D", "Pet Supplies");
      dict1.Add("E", "Clothing");
      dict1.Add("F", "Footwear");
      Console.WriteLine("ListDictionary1 elements...");
      foreach(DictionaryEntry d in dict1) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Is the ListDictionary1 having fixed size? = "+dict1.IsFixedSize);
      Console.WriteLine("If ListDictionary1 read-only? = "+dict1.IsReadOnly);
      Console.WriteLine("Is ListDictionary1 synchronized = "+dict1.IsSynchronized);
      Console.WriteLine("The ListDictionary1 has the key M? = "+dict1.Contains("M"));
      ListDictionary dict2 = new ListDictionary();
      dict2.Add("1", "One");
      dict2.Add("2", "Two");
      dict2.Add("3", "Three");
      dict2.Add("4", "Four");
      dict2.Add("5", "Five");
      dict2.Add("6", "Six");
      Console.WriteLine("\nListDictionary2 key-value pairs...");
      IDictionaryEnumerator demoEnum = dict2.GetEnumerator();
      while (demoEnum.MoveNext())
      Console.WriteLine("Key = " + demoEnum.Key + ", Value = "+ demoEnum.Value);
      Console.WriteLine("Is the ListDictionary2 having fixed size? = "+dict2.IsFixedSize);
      Console.WriteLine("If ListDictionary2 read-only? = "+dict2.IsReadOnly);
      Console.WriteLine("Is ListDictionary2 synchronized = "+dict2.IsSynchronized);
      Console.WriteLine("The ListDictionary2 has the key 5? = "+dict2.Contains("5"));
   }
}

출력

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

ListDictionary1 elements...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Is the ListDictionary1 having fixed size? = False
If ListDictionary1 read-only? = False
Is ListDictionary1 synchronized = False
The ListDictionary1 has the key M? = False
ListDictionary2 key-value pairs...
Key = 1, Value = One
Key = 2, Value = Two
Key = 3, Value = Three
Key = 4, Value = Four
Key = 5, Value = Five
Key = 6, Value = Six
Is the ListDictionary2 having fixed size? = False
If ListDictionary2 read-only? = False
Is ListDictionary2 synchronized = False
The ListDictionary2 has the key 5? = True

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

예시

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      ListDictionary dict1 = new ListDictionary();
      dict1.Add("A", "Books");
      dict1.Add("B", "Electronics");
      dict1.Add("C", "Smart Wearables");
      dict1.Add("D", "Pet Supplies");
      dict1.Add("E", "Clothing");
      dict1.Add("F", "Footwear");
      Console.WriteLine("ListDictionary1 elements...");
      foreach(DictionaryEntry d in dict1) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      ListDictionary dict2 = new ListDictionary();
      dict2.Add("1", "One");
      dict2.Add("2", "Two");
      dict2.Add("3", "Three");
      dict2.Add("4", "Four");
      dict2.Add("5", "Five");
      dict2.Add("6", "Six");
      Console.WriteLine("\nListDictionary2 elements...");
      foreach(DictionaryEntry d in dict2) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      ListDictionary dict3 = new ListDictionary();
      dict3 = dict2;
      Console.WriteLine("\nIs ListDictionary3 equal to ListDictionary2? = "+(dict3.Equals(dict2)));
   }
}

출력

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

ListDictionary1 elements...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
ListDictionary2 elements...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
Is ListDictionary3 equal to ListDictionary2? = True