C#のSortedDictionary.Clear()メソッドとは?使い方とサンプルコードを解説
C#のSortedDictionary<TKey, TValue>.Clear()メソッドは、SortedDictionaryに格納されているすべての要素(キーと値のペア)を一括で削除するために使用されるメソッドです。このメソッドを実行すると、コレクションは空の状態になり、Countプロパティの値は0になります。
構文
Clear()メソッドの構文は以下のとおりです。
public void Clear ();
このメソッドは引数を受け取らず、戻り値もありません。また、SortedDictionaryにおけるClear()メソッドはO(1)の操作として定義されており、要素数に関わらず高速に処理できます。
サンプルコード1
まずは基本的な使用例を見てみましょう。
using System;
using System.Collections;
using System.Collections.Generic;
public class Demo {
public static void Main(){
SortedDictionary<int, string> sortedDict = new SortedDictionary<int, string>();
sortedDict.Add(100, "Mobile");
sortedDict.Add(200, "Laptop");
sortedDict.Add(300, "Desktop");
sortedDict.Add(400, "Speakers");
sortedDict.Add(500, "Headphone");
sortedDict.Add(600, "Earphone");
Console.WriteLine("SortedDictionary key-value pairs...");
IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
Console.WriteLine("Count of SortedDictionary key-value pairs = "+sortedDict.Count);
Console.WriteLine("\nThe SortedDictionary has the key 200? = "+sortedDict.ContainsKey(200));
sortedDict.Clear();
Console.WriteLine("Count of SortedDictionary key-value pairs = "+sortedDict.Count);
}
}実行結果
上記のコードを実行すると、次のような出力が得られます。
SortedDictionary key-value pairs... Key = 100, Value = Mobile Key = 200, Value = Laptop Key = 300, Value = Desktop Key = 400, Value = Speakers Key = 500, Value = Headphone Key = 600, Value = Earphone Count of SortedDictionary key-value pairs = 6 The SortedDictionary has the key 200? = True Count of SortedDictionary key-value pairs = 0
この例では、6つのキーと値のペアを持つSortedDictionaryに対してClear()メソッドを呼び出しています。実行後、Countが0になっていることから、すべての要素が削除されたことが確認できます。
サンプルコード2
続いて、別の例を見てみましょう。こちらでは要素を追加した後にClear()メソッドを使用しています。
using System;
using System.Collections;
using System.Collections.Generic;
public class Demo {
public static void Main(){
SortedDictionary<int, string> sortedDict = new SortedDictionary<int, string>();
sortedDict.Add(100, "Inspiron");
sortedDict.Add(200, "Alienware");
sortedDict.Add(300, "Projectors");
sortedDict.Add(400, "XPS");
Console.WriteLine("SortedDictionary key-value pairs...");
IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
Console.WriteLine("Count of SortedDictionary key-value pairs = "+sortedDict.Count);
sortedDict.Add(800, "Notebook");
sortedDict.Add(10000, "Bluetooth Speaker");
Console.WriteLine("\nSortedDictionary key-value pairs...UPDATED");
demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
Console.WriteLine("Count of SortedDictionary key-value pairs (UPDATED) = "+sortedDict.Count);
sortedDict.Clear();
Console.WriteLine("\nCount of SortedDictionary key-value pairs (UPDATED) = "+sortedDict.Count);
}
}実行結果
上記のコードを実行すると、次のような出力が得られます。
SortedDictionary key-value pairs... Key = 100, Value = Inspiron Key = 200, Value = Alienware Key = 300, Value = Projectors Key = 400, Value = XPS Count of SortedDictionary key-value pairs = 4 SortedDictionary key-value pairs...UPDATED Key = 100, Value = Inspiron Key = 200, Value = Alienware Key = 300, Value = Projectors Key = 400, Value = XPS Key = 800, Value = Notebook Key = 10000, Value = Bluetooth Speaker Count of SortedDictionary key-value pairs (UPDATED) = 6 Count of SortedDictionary key-value pairs (UPDATED) = 0
このように、要素を追加してCountが6になった状態でも、Clear()メソッドを呼び出せばすべての要素が削除され、Countは0に戻ります。SortedDictionaryの内容を完全にリセットしたい場合に、Clear()メソッドは非常に便利です。
-
HTML DOM console.clear()メソッドの使い方を徹底解説!コンソールをクリアする方法
HTML DOMのconsole.clear()メソッドは、ブラウザの開発者コンソールに表示された内容をすべて消去するためのメソッドです。このメソッドを実行すると、コンソール上の以前の出力がすべてクリアされ、「Console was cleared(コンソールはクリアされました)」というメッセージが表示されます。console.clear()メソッドは、Chrome、Firefox、Edge、Safariなど、主要なすべてのブラウザで完全にサポートされています。構文console.clear()メソッドの構文は以下のとおりです。引数は不要で、非常にシンプルに使えます。console.clear
-
HTML DOM Storage key()メソッドの使い方を徹底解説
HTML DOM Storage key()メソッドとはHTML DOMのStorage key()メソッドは、ストレージオブジェクト内の指定したインデックス位置に保存されているキー名を取得するためのメソッドです。取得したいキーの番号(インデックス)は、引数としてkey()メソッドに渡します。このメソッドで利用できるストレージオブジェクトは、sessionStorageオブジェクトとlocalStorageオブジェクトの2種類です。構文Storage key()メソッドの基本構文は以下のとおりです。localStorageの場合localStorage.key(index);sessionSt