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

C#でOrderedDictionaryに特定のキーが含まれているか確認する方法

C#のOrderedDictionaryコレクションに特定のキーが存在するかどうかを確認するには、Contains()メソッドを使用します。このメソッドは、指定したキーがコレクション内に存在すればTrue、存在しなければFalseを返します。

Contains()メソッドの基本構文

public bool Contains(object key);

引数には検索対象のキーを渡します。戻り値はbool型で、キーの有無を判定できます。

例1:数値をキーとした場合

まず、数値文字列をキーとするOrderedDictionaryを作成し、Contains()メソッドでキーの存在を確認してみましょう。

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main(){
      OrderedDictionary dict = new OrderedDictionary();
      dict.Add("1", "One");
      dict.Add("2", "Two");
      dict.Add("3", "Three");
      dict.Add("4", "Four");
      dict.Add("5", "Five");
      dict.Add("6", "Six");
      dict.Add("7", "Seven");
      dict.Add("8", "Eight");
      ICollection col = dict.Values;
      String[] strVal = new String[dict.Count];
      col.CopyTo(strVal, 0);
      Console.WriteLine("値を表示します...");
      for (int i = 0; i < dict.Count; i++) {
         Console.WriteLine(strVal[i]);
      }
      Console.WriteLine("OrderedDictionaryは読み取り専用か? = "+dict.IsReadOnly);
      Console.WriteLine("キー15は存在するか? = "+dict.Contains("15"));
      Console.WriteLine("キー5は存在するか? = "+dict.Contains("5"));
   }
}

出力結果

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

値を表示します...
One
Two
Three
Four
Five
Six
Seven
Eight
OrderedDictionaryは読み取り専用か? = False
キー15は存在するか? = False
キー5は存在するか? = True

実行結果から、登録されていないキー「15」に対してはFalse、登録済みのキー「5」に対してはTrueが返されていることがわかります。また、IsReadOnlyプロパティにより、このコレクションが読み取り専用ではないことも確認できました。

例2:アルファベットをキーとした場合

続いて、別の例として、アルファベットをキーとしたOrderedDictionaryでキーの存在を確認してみます。

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main(){
      OrderedDictionary dict = new OrderedDictionary();
      dict.Add("A", "Pen");
      dict.Add("B", "Pencil");
      dict.Add("C", "Notebook");
      dict.Add("D", "Table");
      dict.Add("E", "Chair");
      dict.Add("F", "Coworking");
      dict.Add("G", "Cubicle");
      dict.Add("H", "Sticky Notes");
      dict.Add("I", "WhiteBoard");
      dict.Add("J", "Marker");
      ICollection col = dict.Values;
      String[] strVal = new String[dict.Count];
      col.CopyTo(strVal, 0);
      Console.WriteLine("値を表示します...");
      for (int i = 0; i < dict.Count; i++) {
         Console.WriteLine(strVal[i]);
      }
      Console.WriteLine("OrderedDictionaryは読み取り専用か? = "+dict.IsReadOnly);
      Console.WriteLine("キーBは存在するか? = "+dict.Contains("B"));
   }
}

出力結果

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

値を表示します...
Pen
Pencil
Notebook
Table
Chair
Coworking
Cubicle
Sticky Notes
WhiteBoard
Marker
OrderedDictionaryは読み取り専用か? = False
キーBは存在するか? = True

まとめ

OrderedDictionaryクラスのContains()メソッドを使うことで、特定のキーがコレクションに存在するかどうかを簡単に判定できます。キーが存在する場合はTrue、存在しない場合はFalseが返されるため、要素へのアクセス前の存在チェックなどに活用すると安全です。

  1. C#のStackに特定の要素が含まれているか確認する方法(Containsメソッド)

    C#のStackに特定の要素が含まれているか確認する方法 C#でStack(スタック)コレクションに特定の要素が含まれているかどうかを調べるには、Contains()メソッドを使用します。Contains()メソッドは、引数に指定した要素がStack内に存在する場合は「True」、存在しない場合は「False」を返します。 以下、実際のコード例を見ていきましょう。 例1:整数型(int)のStack using System; using System.Collections.Generic; public class Demo { public static void Main(){

  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);