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

C#でHashtable(ハッシュテーブル)に特定のキーが存在するかどうかを確認する方法

C#のHashtableに特定のキーが含まれているかどうかを確認するには、Contains()メソッドを使用します。このメソッドは、指定したキーがHashtable内に存在する場合はtrue、存在しない場合はfalseを返します。

以下に具体的なコード例を示します。

サンプルコード1

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", "");
      hash.Add("Five","Harry");
      hash.Add("Six", "F");
      hash.Add("Seven", "Tom");
      hash.Add("Eight","Andy");
      hash.Add("Nine", "I");
      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 the Hashtable having fixed size? = "+hash.IsFixedSize);
      Console.WriteLine("If Hashtable read-only? = "+hash.IsReadOnly);
      Hashtable hash2 = Hashtable.Synchronized(hash);
      Console.WriteLine("Is Hash synchronized = "+hash2.IsSynchronized);
      Console.WriteLine("Does Hashtable contains the key Ten = "+hash.Contains("Ten"));
   }
}

実行結果

上記のコードを実行すると、次のような出力が得られます。

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
Eight and Andy
Nine and I
Six and F
Is the Hashtable having fixed size? = False
If Hashtable read-only? = False
Is Hash synchronized = True
Does Hashtable contains the key Ten = True

この例では、hash.Contains("Ten")によってキー「Ten」の存在を確認しており、結果としてTrueが返されています。また、IsFixedSizeIsReadOnlyなどのプロパティを使えば、Hashtableの状態についても同時に確認できます。

サンプルコード2

続いて、キーが存在しない場合の動作を確認する別の例を見てみましょう。

using System;
using System.Collections;
public class Demo {
   public static void Main(){
      Hashtable hash = new Hashtable();
      hash.Add("1", "AB");
      hash.Add("2", "BC");
      hash.Add("3", "DE");
      hash.Add("4", "EF");
      hash.Add("5", "GH");
      hash.Add("6", "IJ");
      hash.Add("7", "KL");
      hash.Add("8", "MN");
      hash.Add("9", "OP");
      hash.Add("10", "QR");
      Console.WriteLine("Value at key 3 = "+hash["3"]);
      Console.WriteLine("Does Hashtable contains the key 12 = "+hash.Contains("12"));
   }
}

実行結果

上記のコードを実行すると、次のような出力が得られます。

Value at key 3 = DE
Does Hashtable contains the key 12 = False

2つ目の例では、存在するキー「3」に対してはインデクサを使って値「DE」を取得できている一方、存在しないキー「12」をContains()で確認した結果はFalseとなっています。

補足:ContainsKeyメソッドとの違い

HashtableにはContainsKey()というメソッドも用意されており、こちらもContains()と同じくキーの存在確認に使用できます。両者は動作が同一ですが、可読性の観点からは意図が明確なContainsKey()を選ぶのが一般的です。なお、値の存在を確認したい場合にはContainsValue()を使用します。

  1. C#でHashtableが固定サイズかどうかを確認する方法

    C# の Hashtable(ハッシュテーブル)が固定サイズを持っているかどうかを確認するには、IsFixedSize プロパティを使用します。このプロパティは、コレクションが固定サイズの場合に true、要素の追加や削除が可能な可変サイズの場合に false を返します。通常、new Hashtable() で作成した Hashtable は動的にサイズが変更されるため、false が返されます。例1:IsFixedSizeプロパティの基本的な使い方以下のコードでは、初期容量 10 の Hashtable を作成し、複数のキーと値を追加したうえで、IsFixedSize プロパティの値を出力

  2. C#でリストが空かどうかをチェックする方法

    C#では、List<T>を使って要素を格納し、後から取り出すことができます。まずは、リストの基本的な使い方の例を見てみましょう。例using System; using System.Collections.Generic; public class Demo { public static void Main(string[] args) { var subjects = new List<string>(); subjects.Add(Maths); subjects.Add(Java); subjects.A