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

C#のDictionary.Clearメソッドとは?使い方をサンプルコードで解説

C#のDictionary.Clear()メソッドは、Dictionary<TKey, TValue>に格納されたすべてのキーと値のペアを一括で削除するためのメソッドです。呼び出し後はディクショナリが空になり、Countプロパティの値は0になります。

構文

public void Clear();

Clear()メソッドは引数を受け取らず、戻り値もありません。また、このメソッドを実行してもディクショナリの容量(Capacity)は変わりません。メモリ使用量を縮小したい場合は、必要に応じてTrimExcess()メソッドを併用してください。

例1:Clear()メソッドの基本的な使い方

まずは、5つのキーと値のペアを持つディクショナリに対してClear()メソッドを呼び出す基本例を見てみましょう。

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main(){
        Dictionary<string, string> dict =
        new Dictionary<string, string>();
        dict.Add("One", "John");
        dict.Add("Two", "Tom");
        dict.Add("Three", "Jacob");
        dict.Add("Four", "Kevin");
        dict.Add("Five", "Nathan");

        Console.WriteLine("Count of elements = "+dict.Count);
        Console.WriteLine("\nKey/value pairs...");
        foreach(KeyValuePair<string, string> res in dict){
            Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
        }

        dict.Clear();

        Console.WriteLine("Cleared Key/value pairs...");
        foreach(KeyValuePair<string, string> res in dict){
            Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
        }
        Console.WriteLine("Count of elements now = "+dict.Count);
    }
}

実行結果

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

Count of elements = 5
Key/value pairs...
Key = One, Value = John
Key = Two, Value = Tom
Key = Three, Value = Jacob
Key = Four, Value = Kevin
Key = Five, Value = Nathan
Cleared Key/value pairs...
Count of elements now = 0

Clear()メソッドの呼び出し後は、foreachループで何も表示されず、要素数が0になっていることが確認できます。

例2:要素を追加してからクリアする場合

次に、ディクショナリ作成後にさらに要素を追加し、その状態でClear()メソッドを使用する例を見てみましょう。

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main(){
        Dictionary<string, string> dict =
        new Dictionary<string, string>();
        dict.Add("One", "John");
        dict.Add("Two", "Tom");
        dict.Add("Three", "Jacob");
        dict.Add("Four", "Kevin");
        dict.Add("Five", "Nathan");

        Console.WriteLine("Count of elements = "+dict.Count);

        dict.Add("Six", "Anne");
        dict.Add("Seven", "Katoe");
        Console.WriteLine("Count of elements (updated) = "+dict.Count);

        Console.WriteLine("Key/value pairs...");
        foreach(KeyValuePair<string, string> res in dict){
            Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
        }

        dict.Clear();

        Console.WriteLine("Cleared Key/value pairs...");
        foreach(KeyValuePair<string, string> res in dict){
            Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
        }
    }
}

実行結果

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

Count of elements = 5
Count of elements (updated) = 7
Key/value pairs...
Key = One, Value = John
Key = Two, Value = Tom
Key = Three, Value = Jacob
Key = Four, Value = Kevin
Key = Five, Value = Nathan
Key = Six, Value = Anne
Key = Seven, Value = Katoe
Cleared Key/value pairs...

まとめ

  • Dictionary.Clear()は、ディクショナリ内のすべてのキーと値のペアを削除するメソッドです。
  • 引数も戻り値もなく、呼び出し後はCountが0になります。
  • 容量(Capacity)は維持されるため、メモリを縮小したい場合はTrimExcess()を活用しましょう。
  • 計算量はO(n)(nはディクショナリの容量)であり、大量のデータを扱う場合でも高速に動作します。
  1. HTML CanvasのclearRect()メソッドの使い方を徹底解説

    HTML CanvasのclearRect()メソッドは、指定した矩形領域内のピクセルを消去するために使用されるメソッドです。<canvas>要素を使用すると、JavaScriptを通じてWebページ上に自由にグラフィックを描画できます。すべてのcanvas要素には、描画領域のサイズを決定するwidth(幅)とheight(高さ)という2つの属性が用意されています。 clearRect()メソッドの基本構文 clearRect()メソッドの構文は以下の通りです。 ctx.clearRect(p, q, width, height); 各パラメータの意味は次のようになっています。

  2. HTML DOM console.clear()メソッドの使い方を徹底解説!コンソールをクリアする方法

    HTML DOMのconsole.clear()メソッドは、ブラウザの開発者コンソールに表示された内容をすべて消去するためのメソッドです。このメソッドを実行すると、コンソール上の以前の出力がすべてクリアされ、「Console was cleared(コンソールはクリアされました)」というメッセージが表示されます。console.clear()メソッドは、Chrome、Firefox、Edge、Safariなど、主要なすべてのブラウザで完全にサポートされています。構文console.clear()メソッドの構文は以下のとおりです。引数は不要で、非常にシンプルに使えます。console.clear