C#
 Computer >> コンピューター >  >> プログラミング >> C#

C#のOrderedDictionaryクラス


OrderedDictionaryクラスは、キーまたはインデックスからアクセスできるキーと値のペアのコレクションを表します。

OrderedDictionaryクラスのプロパティは次のとおりです-

Sr.no プロパティと説明
1 カウント
OrderedDictionaryコレクションに含まれるキーと値のペアの数を取得します。
2 IsReadOnly
OrderedDictionaryコレクションが読み取り専用かどうかを示す値を取得します。
3 アイテム[Int32]
指定されたインデックスの値を取得または設定します。
4 アイテム[オブジェクト]
指定されたキーで値を取得または設定します。
5 キー
OrderedDictionaryコレクションのキーを含むICollectionオブジェクトを取得します。
6
OrderedDictionaryコレクションの値を含むICollectionオブジェクトを取得します。

以下は、OrderedDictionaryクラスのメソッドの一部です-

Sr.no メソッドと説明
1 Add(Object、Object)
指定されたキーと値を持つエントリを、使用可能なインデックスが最も低いOrderedDictionaryコレクションに追加します。
2 AsReadOnly()
現在のOrderedDictionaryコレクションの読み取り専用コピーを返します。
3 Clear()
OrderedDictionaryコレクションからすべての要素を削除します。
4 contains(Object)
OrderedDictionaryコレクションに特定のキーが含まれているかどうかを判別します。
5 CopyTo(Array、Int32)
OrderedDictionary要素を指定されたインデックスの1次元配列オブジェクトにコピーします。
6 Equals(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

  1. C#のコンソールクラス

    C#のConsoleクラスは、コンソールアプリケーションの標準の入力、出力、およびエラーストリームを表すために使用されます。 C#のコンソールクラスプロパティの例をいくつか見てみましょう- Console.CursorLeftプロパティ C#でコンソールのCursorLeftを変更するには、Console.CursorLeftプロパティを使用します。 例 例を見てみましょう- using System; class Demo {    public static void Main (string[] args) {       Cons

  2. Pythonでのオブジェクト指向プログラミング?

    Pythonは、その存在以来、オブジェクト指向プログラミング言語でした。クラスとオブジェクトは、オブジェクト指向プログラミングの2つの主要な構成要素です。 クラスは、オブジェクトがクラスのインスタンスである新しいタイプのオブジェクトを作成します。 最も単純なクラスの1つを作成しましょう Pythonでクラスを定義する 空のクラスを定義しましょう。 #Define a class class Vehicle():    pass # An empty block # Instantiating objects v = Vehicle() print(v) 結果 <