【C#】HybridDictionaryに特定のキーが含まれているか確認する方法(Containsメソッド)
C#のHybridDictionaryクラスには、指定したキーがディクショナリ内に存在するかどうかを判定するためのContainsメソッドが用意されています。キーが存在する場合は true、存在しない場合は false を返します。
なお、HybridDictionaryは System.Collections.Specialized 名前空間に属するコレクションで、要素数が少ない間はListDictionaryとして動作し、要素が増えると自動的にHashtableへ切り替わるという特徴を持っています。
Containsメソッドの構文
public bool Contains(object key);
例1:特定のキーの存在を確認する
まずは基本的な使い方から見ていきましょう。次のコードでは、5つのキーと値のペアをHybridDictionaryに追加し、キー「C」が含まれているかどうかをContainsメソッドで確認しています。
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
HybridDictionary dict = new HybridDictionary(5);
dict.Add("A", "AB");
dict.Add("B", "BC");
dict.Add("C", "DE");
dict.Add("D", "FG");
dict.Add("E", "HI");
Console.WriteLine("Key/Value pairs...");
foreach(DictionaryEntry d in dict)
Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value);
Console.WriteLine("Does HybridDictionary contains the key C? = "+dict.Contains("C"));
}
}
出力結果
上記のコードを実行すると、次のような出力が得られます。
Key/Value pairs... Key = A, Value = AB Key = B, Value = BC Key = C, Value = DE Key = D, Value = FG Key = E, Value = HI Does HybridDictionary contains the key C? = True
キー「C」はディクショナリに登録されているため、Containsメソッドは True を返しています。
例2:複数のHybridDictionaryに対してキーを確認する
続いて、もう少し実践的な例を見てみましょう。2つのHybridDictionaryを作成し、それぞれの要素数を取得したうえで、存在しないキーを指定した場合の動作や、Clearメソッドによる全削除後の変化も確認します。
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
HybridDictionary dict1 = new HybridDictionary();
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("HybridDictionary1 elements...");
foreach(DictionaryEntry d in dict1){
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("Count of Key/value pairs in Dictionary1 = "+dict1.Count);
HybridDictionary dict2 = new HybridDictionary();
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("\nHybridDictionary2 elements...");
foreach(DictionaryEntry d in dict2){
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("Count of Key/value pairs in Dictionary2 = "+dict1.Count);
Console.WriteLine("Does HybridDictionary2 contains the key 10? = "+dict2.Contains(10));
dict2.Clear();
Console.WriteLine("Count of Key/value pairs in Dictionary2 (Updated) = "+dict2.Count);
}
}
出力結果
実行すると、以下の出力が表示されます。
HybridDictionary1 elements... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear Count of Key/value pairs in Dictionary1 = 6 HybridDictionary2 elements... 1 One 2 Two 3 Three 4 Four 5 Five 6 Six Count of Key/value pairs in Dictionary2 = 6 Does HybridDictionary2 contains the key 10? = False Count of Key/value pairs in Dictionary2 (Updated) = 0
ポイントまとめ
- Containsメソッドを使うと、HybridDictionaryに指定したキーが存在するかどうかを簡単に判定できます。
- キーが存在すれば
true、存在しなければfalseが返されます。 - 例2のように、存在しないキー「10」を指定した場合は
Falseが返されることが確認できます。 - Clearメソッドを呼び出すとすべての要素が削除され、Countプロパティの値が0になります。
-
【C#】SortedSetに特定の要素が含まれているか確認する方法(Containsメソッドの使い方)
C#のSortedSet<T>クラスには、指定した要素がセット内に存在するかどうかを判定するためのContainsメソッドが用意されています。この記事では、Containsメソッドを使った具体的なコード例とその実行結果を紹介します。ContainsメソッドとはContains(T item)は、SortedSet内に指定した要素が存在する場合はtrue、存在しない場合はfalseを返すメソッドです。SortedSetは内部でソートされた状態を維持しているため、高速な検索が可能です。サンプルコード1:文字列のSortedSetで要素を検索するまずは、文字列型のSortedSetに対し
-
C#のCollectionに要素が含まれているかどうかを確認する方法
C#でコレクション(Collection)内に特定の要素が存在するかどうかを確認するには、Contains()メソッドを使用します。このメソッドは、指定した要素がコレクション内に見つかった場合は true、見つからなかった場合は false を返します。以下に具体的なコード例を示します。例1:整数のコレクションの場合using System; using System.Collections.ObjectModel; public class Demo { public static void Main(){