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

【C#】Hashtableに特定のキーが含まれているか確認する方法(ContainsKeyメソッド)

C#のHashtable(ハッシュテーブル)に特定のキーが存在するかどうかを確認するには、ContainsKey()メソッドを使用します。このメソッドは、指定したキーがHashtable内に存在すれば true、存在しなければ false を返します。キーの存在チェックは、値を取得する前のエラー防止などによく使われる基本的な操作です。

サンプルコード1

以下の例では、文字列のキーと値を持つHashtableを作成し、ContainsKey()メソッドでキー「Seven」が存在するかどうかを確認しています。

using System;
using System.Collections;

public class Demo {
   public static void Main() {
      Hashtable hash = new Hashtable();
      hash.Add("One", "Katie");
      hash.Add("Two", "John");
      hash.Add("Three", "Barry");
      hash.Add("Four", "Mark");
      hash.Add("Five", "Harry");
      hash.Add("Six", "Nathan");
      hash.Add("Seven", "Tom");
      hash.Add("Eight", "Andy");
      hash.Add("Nine", "Illeana");
      hash.Add("Ten", "Tim");

      Console.WriteLine("Hashtable Key and Value pairs...");
      foreach(DictionaryEntry entry in hash) {
         Console.WriteLine("{0} and {1}", entry.Key, entry.Value);
      }

      Console.WriteLine("Is Hashtable having fixed size? = " + hash.IsFixedSize);
      Console.WriteLine("If Hashtable read-only? = " + hash.IsReadOnly);
      Console.WriteLine("The Hashtable consists of the key? = " + hash.ContainsKey("Seven"));
   }
}

実行結果

Hashtable Key and Value pairs...
One and Katie
Ten and Tim
Five and Harry
Three and Barry
Seven and Tom
Two and John
Four and Mark
Eight and Andy
Nine and Illeana
Six and Nathan
Is Hashtable having fixed size? = False
If Hashtable read-only? = False
The Hashtable consists of the key? = True

実行結果から、hash.ContainsKey("Seven")True を返していることがわかります。また、Hashtableは要素数が固定されておらず(IsFixedSize = False)、読み取り専用でもない(IsReadOnly = False)ことも確認できます。

サンプルコード2

次に、数字をキーとして使用した別の例を見てみましょう。ここでは、キー「5」が存在するかどうかを確認しています。

using System;
using System.Collections;

public class Demo {
   public static void Main() {
      Hashtable hash = new Hashtable();
      hash.Add("1", "A");
      hash.Add("2", "B");
      hash.Add("3", "C");
      hash.Add("4", "D");
      hash.Add("5", "E");
      hash.Add("6", "F");
      hash.Add("7", "G");
      hash.Add("8", "H");
      hash.Add("9", "I");
      hash.Add("10", "J");

      Console.WriteLine("Hashtable Key and Value pairs...");
      foreach(DictionaryEntry entry in hash) {
         Console.WriteLine("{0} and {1}", entry.Key, entry.Value);
      }

      Console.WriteLine("Is Hashtable having fixed size? = " + hash.IsFixedSize);
      Console.WriteLine("If Hashtable read-only? = " + hash.IsReadOnly);
      Console.WriteLine("The Hashtable consists of the key? = " + hash.ContainsKey("5"));
   }
}

実行結果

Hashtable Key and Value pairs...
10 and J
1 and A
2 and B
3 and C
4 and D
5 and E
6 and F
7 and G
8 and H
9 and I
Is Hashtable having fixed size? = False
If Hashtable read-only? = False
The Hashtable consists of the key? = True

ポイントまとめ

  • ContainsKey(Object key):指定したキーがHashtableに存在するかを判定し、bool値を返します。
  • Hashtableの要素はハッシュ関数に基づいて格納されるため、foreachで列挙する際の順序は追加順とは限りません。
  • 類似メソッドとして ContainsValue()(値の存在確認)や Contains()(ContainsKeyと同等)もあります。
  • 新しい.NETプロジェクトでは、型安全な Dictionary<TKey, TValue> の使用が推奨されます。
  1. 【C#】SortedSetに特定の要素が含まれているか確認する方法(Containsメソッドの使い方)

    C#のSortedSet<T>クラスには、指定した要素がセット内に存在するかどうかを判定するためのContainsメソッドが用意されています。この記事では、Containsメソッドを使った具体的なコード例とその実行結果を紹介します。ContainsメソッドとはContains(T item)は、SortedSet内に指定した要素が存在する場合はtrue、存在しない場合はfalseを返すメソッドです。SortedSetは内部でソートされた状態を維持しているため、高速な検索が可能です。サンプルコード1:文字列のSortedSetで要素を検索するまずは、文字列型のSortedSetに対し

  2. C#でHashtable(ハッシュテーブル)に特定の値が含まれているか確認する方法

    C#のHashtableに特定の値(Value)が含まれているかどうかを確認するには、ContainsValueメソッドを使用します。このメソッドは、指定した値がHashtable内に存在する場合はtrue、存在しない場合はfalseを返します。以下に具体的なコード例を示します。サンプルコード1using System; using System.Collections; public class Demo { public static void Main(){ Hashtable hash = new Hashtable(); hash.Add(1, A);