C#でOrderedDictionaryの読み取り専用コピーを取得する方法
C#のOrderedDictionary(System.Collections.Specialized名前空間)から読み取り専用のコピーを取得するには、AsReadOnly()メソッドを使用します。このメソッドは、要素の追加・削除・変更ができない読み取り専用ラッパーを返し、そのIsReadOnlyプロパティはtrueになります。
AsReadOnly()メソッドとは
AsReadOnly()は、呼び出し元のOrderedDictionaryと同じキーと値を持つ、読み取り専用の新しいインスタンスを生成します。元のコレクション自体は引き続き変更可能である点に注意してください。誤ってデータを書き換えたくない場面や、外部に安全にコレクションを公開したい場面で特に役立ちます。
例1:2つのOrderedDictionaryを作成して読み取り専用化する
以下の例では、商品カテゴリを格納したdict1と数値を格納したdict2の2つのコレクションを作成し、dict2に対してAsReadOnly()を呼び出しています。あわせて、Equals()メソッドによる2つのコレクションの比較結果も確認できます。
サンプルコード
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
OrderedDictionary dict1 = new OrderedDictionary();
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("OrderedDictionary1 elements...");
foreach(DictionaryEntry d in dict1){
Console.WriteLine(d.Key + " " + d.Value);
}
OrderedDictionary dict2 = new OrderedDictionary();
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("\nOrderedDictionary2 elements...");
foreach(DictionaryEntry d in dict2){
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("\nIs OrderedDictionary1 equal to OrderedDictionary2? = "+(dict1.Equals(dict2)));
OrderedDictionary orderedDict = dict2.AsReadOnly();
Console.WriteLine("Is OrderedDictionary read-only? = "+orderedDict.IsReadOnly);
}
}
出力結果
上記のコードを実行すると、次のような出力が得られます。
OrderedDictionary1 elements... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear OrderedDictionary2 elements... 1 One 2 Two 3 Three 4 Four 5 Five 6 Six Is OrderedDictionary1 equal to OrderedDictionary2? = False Is OrderedDictionary read-only? = True
dict1とdict2は格納されている内容が異なるため、Equals()の比較結果はFalseになっています。一方、AsReadOnly()で取得したorderedDictのIsReadOnlyプロパティはTrueであり、読み取り専用として扱われていることがわかります。
例2:単一のOrderedDictionaryを読み取り専用にする
続いて、よりシンプルな例を見てみましょう。1つの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);
}
OrderedDictionary orderedDict = dict.AsReadOnly();
Console.WriteLine("Is OrderedDictionary read-only? = "+orderedDict.IsReadOnly);
}
}
出力結果
OrderedDictionary elements... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear Is OrderedDictionary read-only? = True
まとめ
AsReadOnly()メソッドを使うと、OrderedDictionaryの読み取り専用コピーを簡単に作成できます。- コレクションが読み取り専用かどうかは、
IsReadOnlyプロパティで確認できます(読み取り専用の場合はtrueを返します)。 - 元のコレクションは変更可能なまま残るため、「内部では更新しつつ、外部には読み取り専用として公開する」といった設計にも活用できます。
-
C#でListの要素範囲を取得する方法:GetRange()メソッドの使い方
C#のListで特定の範囲の要素を取得するには、GetRange() メソッドを使用します。このメソッドは、指定したインデックス位置から指定した数の要素を取り出し、新しいリストとして返します。 GetRange() メソッドの基本構文 public List<T> GetRange(int index, int count); index:取得を開始する要素のインデックス(0から始まります) count:取得する要素の数 リストの作成と要素の追加 まず、リストを作成して要素を追加します。 List<int> arr1 = new List<int>();
-
【C#】Dictionaryからキーのリストを取得する方法
C#では、Dictionary<TKey, TValue> に格納されたすべてのキーを、List<TKey> として簡単に取り出すことができます。この記事では、Keys プロパティを使ってキーの一覧をリスト化し、画面に表示するまでの手順をサンプルコード付きで解説します。 Dictionaryに要素を追加する まずは、辞書(Dictionary)に要素を設定しましょう。 Dictionary<int, string> d = new Dictionary<int, string>(); // 辞書の要素 d.Add(1, "One&q