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

C#のListDictionaryからすべてのエントリを削除する方法(Clearメソッドの使い方)

C#のListDictionaryからすべてのエントリ(キーと値のペア)を一括で削除するには、Clear()メソッドを使用します。このメソッドを呼び出すと、コレクション内の全要素が削除され、要素数(Count)は0になります。

以下に、具体的なサンプルコードと実行結果を示します。

サンプルコード1:Clear()メソッドで全エントリを削除する

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);
      }
      Console.WriteLine("Count of key/value pairs in Dictionary 2 = "+dict2.Count);

      ListDictionary dict3 = new ListDictionary();
      dict3 = dict2;
      Console.WriteLine("\nIs ListDictionary3 equal to ListDictionary2? = "+(dict3.Equals(dict2)));

      // すべてのエントリを削除
      dict3.Clear();
      Console.WriteLine("Count of key/value pairs in Dictionary 3 = "+dict3.Count);
   }
}

実行結果

上記のコードを実行すると、次の出力が得られます。

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
Count of key/value pairs in Dictionary 2 = 6
Is ListDictionary3 equal to ListDictionary2? = True
Count of key/value pairs in Dictionary 3 = 0

コードのポイント

  • dict3 = dict2; により、dict3はdict2と同じオブジェクトを参照しています。そのため Equals() の比較結果は True になります。
  • dict3.Clear(); を呼び出した後、要素数が 0 になっていることが確認できます。
  • なお、参照型であるため、dict3への操作はdict2にも影響することに注意してください。

サンプルコード2:要素数(Countプロパティ)の確認

続いて、ListDictionaryに格納されたキーと値のペアの数を Count プロパティで確認する例を見てみましょう。

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("Count of key/value pairs in Dictionary 1 = "+dict1.Count);
   }
}

実行結果

上記のコードを実行すると、次の出力が得られます。

ListDictionary1 elements...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Count of key/value pairs in Dictionary 1 = 6

まとめ

ListDictionaryの全エントリを削除したい場合は、Clear() メソッドを使うのが最も簡単です。また、現在格納されている要素数を知りたい場合は Count プロパティを利用できます。これらを組み合わせれば、コレクションの状態管理が容易になります。

  1. C#のListから重複要素を削除する方法(Distinctメソッドの使い方)

    C#でリスト(List)から重複する要素を削除したい場合は、LINQが提供する Distinct() メソッドを使うのが最も簡単です。この記事では、基本的な使い方から実行例まで、コード付きでわかりやすく解説します。 Distinct()メソッドとは Distinct() は System.Linq 名前空間に含まれる拡張メソッドで、シーケンス内の重複した要素を取り除き、一意な要素だけを返します。戻り値は IEnumerable<T> なので、ToList() を呼び出すことで再びリストとして扱えます。 サンプルリストの作成 まずは、重複を含む整数型のリストを作成します。 List&

  2. JavaでArrayListのすべての要素を削除する方法(clearメソッドの使い方)

    JavaのArrayListからすべての要素を一括で削除したい場合は、clear()メソッドを使用します。この記事では、実際のコード例を通じて、ArrayListの全要素を削除する手順をわかりやすく解説します。1. 要素を持つArrayListを作成するまず、複数の要素を格納したArrayListを用意しましょう。以下の例では、Integer型のArrayListに7つの数値を追加しています。ArrayList<Integer> arrlist = new ArrayList<Integer>(5); arrlist.add(25); arrlist.add(50);