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

C#의 OrderedDictionary 클래스

<시간/>

OrderedDictionary 클래스는 키 또는 인덱스에서 액세스할 수 있는 키/값 쌍의 컬렉션을 나타냅니다.

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

Sr.no 속성 및 설명
1 카운트
OrderedDictionary 컬렉션에 포함된 키/값 쌍의 수를 가져옵니다.
2 읽기 전용
OrderedDictionary 컬렉션이 읽기 전용인지 여부를 나타내는 값을 가져옵니다.
3 항목[Int32]
지정된 인덱스의 값을 가져오거나 설정합니다.
4 항목[객체]
지정된 키로 값을 가져오거나 설정합니다.
5
OrderedDictionary 컬렉션의 키를 포함하는 ICollection 개체를 가져옵니다.
6
OrderedDictionary 컬렉션의 값을 포함하는 ICollection 개체를 가져옵니다.

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

Sr.no 방법 및 설명
1 추가(객체, 객체)
지정된 키와 값이 있는 항목을 사용 가능한 인덱스가 가장 낮은 OrderedDictionary 컬렉션에 추가합니다.
2 AsReadOnly()
현재 OrderedDictionary 컬렉션의 읽기 전용 복사본을 반환합니다.
3 지우기()
OrderedDictionary 컬렉션에서 모든 요소를 ​​제거합니다.
4 포함(객체)
OrderedDictionary 컬렉션에 특정 키가 포함되어 있는지 여부를 결정합니다.
5 CopyTo(배열, Int32)
OrderedDictionary 요소를 지정된 인덱스의 1차원 Array 개체에 복사합니다.
6 같음(객체)
지정된 개체가 현재 개체와 같은지 여부를 확인합니다. (Object에서 상속됨)
7 GetEnumerator()
OrderedDictionary 컬렉션을 반복하는 IDictionaryEnumerator 개체를 반환합니다.

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

예시

OrderedDictionary에 포함된 키/값 쌍의 수를 얻으려면 코드는 다음과 같습니다. -

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      OrderedDictionary dict = new OrderedDictionary();
      dict.Add("A", "Home Appliances");
      dict.Add("B", "Electronics");
      dict.Add("C", "Smart Wearables");
      dict.Add("D", "Pet Supplies");
      dict.Add("E", "Clothing");
      dict.Add("F", "Footwear");
      Console.WriteLine("OrderedDictionary elements...");
      foreach(DictionaryEntry d in dict) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Count of elements in OrderedDictionary = " + dict.Count);
      dict.Clear();
      Console.WriteLine("Count of elements in OrderedDictionary (Updated)= " + dict.Count);
   }
}

출력

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

OrderedDictionary elements...
A Home Appliances
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Count of elements in OrderedDictionary = 6
Count of elements in OrderedDictionary (Updated)= 0

예시

OrderedDictionary에서 모든 요소를 ​​제거하려면 코드는 다음과 같습니다. -

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      OrderedDictionary dict = new OrderedDictionary();
      dict.Add("A", "Books");
      dict.Add("B", "Electronics");
      dict.Add("C", "Smart Wearables");
      dict.Add("D", "Pet Supplies");
      dict.Add("E", "Clothing");
      dict.Add("F", "Footwear");
      Console.WriteLine("OrderedDictionary elements...");
      foreach(DictionaryEntry d in dict) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Count of elements in OrderedDictionary = " + dict.Count);
      dict.Clear();
      Console.WriteLine("Count of elements in OrderedDictionary (Updated)= " + dict.Count);
   }
}

출력

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

OrderedDictionary elements...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Count of elements in OrderedDictionary = 6
Count of elements in OrderedDictionary (Updated)= 0