C#のListDictionaryクラスとは?主なプロパティ・メソッドと使い方を解説
ListDictionaryクラスは、単方向リンクリスト(片方向連結リスト)を使用してIDictionaryインターフェースを実装したコレクションです。要素数が10個未満程度の小規模なコレクションを扱う場合に特に適しており、少ないデータ量であれば高速な動作が期待できます。
この記事では、ListDictionaryクラスの主なプロパティやメソッド、そして実際のコード例を通じてその基本的な使い方を詳しく解説します。
ListDictionaryクラスの主なプロパティ
ListDictionaryクラスには、以下のようなプロパティが用意されています。
| 番号 | プロパティと説明 |
|---|---|
| 1 | Count ListDictionaryに格納されているキー/値ペアの数を取得します。 |
| 2 | IsFixedSize ListDictionaryが固定サイズかどうかを示す値を取得します。 |
| 3 | IsReadOnly ListDictionaryが読み取り専用かどうかを示す値を取得します。 |
| 4 | IsSynchronized ListDictionaryが同期されている(スレッドセーフである)かどうかを示す値を取得します。 |
| 5 | Item[Object] 指定されたキーに関連付けられた値を取得または設定します。 |
| 6 | Keys ListDictionary内のすべてのキーを含むICollectionを取得します。 |
| 7 | SyncRoot ListDictionaryへのアクセスを同期するために使用できるオブジェクトを取得します。 |
| 8 | Values ListDictionary内のすべての値を含むICollectionを取得します。 |
ListDictionaryクラスの主なメソッド
次に、ListDictionaryクラスでよく使用されるメソッドを紹介します。
| 番号 | メソッドと説明 |
|---|---|
| 1 | Add(Object, Object) 指定されたキーと値を持つエントリをListDictionaryに追加します。 |
| 2 | Clear() ListDictionaryからすべてのエントリを削除します。 |
| 3 | Contains(Object) ListDictionaryに特定のキーが含まれているかどうかを判定します。 |
| 4 | CopyTo(Array, Int32) ListDictionaryのエントリを、指定されたインデックス位置から1次元のArrayインスタンスへコピーします。 |
| 5 | Equals(Object) 指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判定します。(Objectから継承) |
| 6 | GetEnumerator() ListDictionaryを反復処理するためのIDictionaryEnumeratorを返します。 |
| 7 | GetHashCode() 既定のハッシュ関数として機能します。(Objectから継承) |
| 8 | GetType() 現在のインスタンスのTypeを取得します。(Objectから継承) |
コード例:読み取り専用かどうかの確認
それでは、実際のコード例を見ていきましょう。まずは、ListDictionaryが読み取り専用かどうかを確認する例です。
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("Is the ListDictionary1 having fixed size? = "+dict1.IsFixedSize);
Console.WriteLine("If ListDictionary1 read-only? = "+dict1.IsReadOnly);
Console.WriteLine("Is ListDictionary1 synchronized = "+dict1.IsSynchronized);
Console.WriteLine("The ListDictionary1 has the key M? = "+dict1.Contains("M"));
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("
ListDictionary2 key-value pairs...");
IDictionaryEnumerator demoEnum = dict2.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = "+ demoEnum.Value);
Console.WriteLine("Is the ListDictionary2 having fixed size? = "+dict2.IsFixedSize);
Console.WriteLine("If ListDictionary2 read-only? = "+dict2.IsReadOnly);
Console.WriteLine("Is ListDictionary2 synchronized = "+dict2.IsSynchronized);
Console.WriteLine("The ListDictionary2 has the key 5? = "+dict2.Contains("5"));
}
}出力結果
上記のコードを実行すると、次のような出力が得られます。
ListDictionary1 elements... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear Is the ListDictionary1 having fixed size? = False If ListDictionary1 read-only? = False Is ListDictionary1 synchronized = False The ListDictionary1 has the key M? = False ListDictionary2 key-value pairs... Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Key = 5, Value = Five Key = 6, Value = Six Is the ListDictionary2 having fixed size? = False If ListDictionary2 read-only? = False Is ListDictionary2 synchronized = False The ListDictionary2 has the key 5? = True
この出力から、ListDictionaryは既定では固定サイズでも読み取り専用でもなく、同期もされていないことがわかります。また、Contains()メソッドを使えば、特定のキーが存在するかどうかを簡単に確認できます。
コード例:2つのListDictionaryオブジェクトが等しいかどうかの確認
続いて、2つのListDictionaryオブジェクトが等しいかどうかを確認する例です。
Example
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("
ListDictionary2 elements...");
foreach(DictionaryEntry d in dict2) {
Console.WriteLine(d.Key + " " + d.Value);
}
ListDictionary dict3 = new ListDictionary();
dict3 = dict2;
Console.WriteLine("
Is ListDictionary3 equal to ListDictionary2? = "+(dict3.Equals(dict2)));
}
}出力結果
上記のコードを実行すると、次のような出力が得られます。
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 Is ListDictionary3 equal to ListDictionary2? = True
ここでは、dict3にdict2の参照を代入しているため、Equals()メソッドによる比較結果はTrueとなります。これは両者が同じオブジェクトを参照しているためです。
まとめ
ListDictionaryクラスは、単方向リンクリストベースの軽量なIDictionary実装であり、要素数が少ないコレクションの管理に最適です。プロパティやメソッドを活用することで、キーの存在確認や列挙処理なども柔軟に行えます。小規模なデータを扱う場面では、Hashtableなどの他のコレクションと比較して検討してみるとよいでしょう。
-
C#のConsoleクラスとは?主要プロパティの使い方を実例付きで解説
C#のConsoleクラスは、コンソールアプリケーションにおける標準入力・標準出力・標準エラー出力の各ストリームを表すクラスです。テキストの表示や入力の読み取りだけでなく、文字色や背景色の変更、カーソル位置やサイズの制御など、コンソール操作に関するさまざまな機能が提供されています。ここでは、Consoleクラスの代表的なプロパティの使い方を、サンプルコードと実行結果とあわせて解説します。Console.CursorLeftプロパティコンソール上のカーソル位置(左端から数えた列位置)を変更するには、Console.CursorLeftプロパティを使用します。サンプルコードusing System
-
Pythonのオブジェクト指向プログラミング入門:クラス・インスタンス・カプセル化の基礎を解説
Pythonは、誕生当初からオブジェクト指向プログラミング言語として設計されてきました。クラスとオブジェクトは、オブジェクト指向プログラミングにおける2つの主要な構成要素です。クラスは新しい型のオブジェクトを作り出すための設計図であり、そのクラスから生成された個々の実体が「インスタンス(オブジェクト)」と呼ばれます。それでは、最もシンプルなクラスを作成してみましょう。Pythonでクラスを定義するまず、空のクラスを定義してみます。# クラスを定義する class Vehicle(): pass # 空のブロック # オブジェクトのインスタンス化 v = Vehicle() prin