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

C#でSortedDictionaryに指定したキーが存在するかどうかを確認する方法

C#のSortedDictionaryに特定のキーが含まれているかどうかを確認するには、ContainsKeyメソッドを使用します。このメソッドは、指定したキーがディクショナリ内に存在する場合はtrue、存在しない場合はfalseを返します。

以下に、具体的なコード例と実行結果を示します。

例1:int型のキーで確認する場合

using System;
using System.Collections;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      SortedDictionary<int, string> sortedDict = new SortedDictionary<int, string>();
      sortedDict.Add(100, "Mobile");
      sortedDict.Add(200, "Laptop");
      sortedDict.Add(300, "Desktop");
      sortedDict.Add(400, "Speakers");
      sortedDict.Add(500, "Headphone");
      sortedDict.Add(600, "Earphone");
      Console.WriteLine("SortedDictionary key-value pairs...");
      IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
      while (demoEnum.MoveNext())
      Console.WriteLine("Key = " + demoEnum.Key + ", Value = "+ demoEnum.Value);
      Console.WriteLine("\nThe SortedDictionary has the key 200? = "+sortedDict.ContainsKey(200));
   }
}

出力結果

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

SortedDictionary key-value pairs...
Key = 100, Value = Mobile
Key = 200, Value = Laptop
Key = 300, Value = Desktop
Key = 400, Value = Speakers
Key = 500, Value = Headphone
Key = 600, Value = Earphone
The SortedDictionary has the key 200? = True

この例では、キー200SortedDictionaryに存在するため、ContainsKey(200)Trueを返しています。また、SortedDictionaryはキーが自動的にソートされて格納される点にも注目してください。

例2:string型のキーで確認する場合

続いて、文字列型のキーを使用した別の例を見てみましょう。

using System;
using System.Collections;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      SortedDictionary<string, string> sortedDict = new SortedDictionary<string, string>();
      sortedDict.Add("A", "John");
      sortedDict.Add("B", "Andy");
      sortedDict.Add("C", "Tim");
      sortedDict.Add("D", "Ryan");
      sortedDict.Add("E", "Kevin");
      sortedDict.Add("F", "Katie");
      sortedDict.Add("G", "Brad");
      Console.WriteLine("SortedDictionary key-value pairs...");
      IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
      while (demoEnum.MoveNext())
      Console.WriteLine("Key = " + demoEnum.Key + ", Value = "+ demoEnum.Value);
      Console.WriteLine("\nThe SortedDictionary has the key F? = "+sortedDict.ContainsKey("F"));
   }
}

出力結果

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

SortedDictionary key-value pairs...
Key = A, Value = John
Key = B, Value = Andy
Key = C, Value = Tim
Key = D, Value = Ryan
Key = E, Value = Kevin
Key = F, Value = Katie
Key = G, Value = Brad
The SortedDictionary has the key F? = True

まとめ

SortedDictionary.ContainsKeyメソッドを使うことで、指定したキーの存在有無を簡単に判定できます。キーが存在しない状態で値にアクセスしようとすると例外が発生するため、事前にContainsKeyでチェックしておくことで、安全なコードを書くことができます。なお、キーではなく値の存在を確認したい場合は、ContainsValueメソッドを使用します。

  1. C#でHashSetに指定された要素が含まれているか確認する方法

    C#のHashSet<T>に特定の要素が含まれているかどうかを確認するには、Contains()メソッドを使用します。このメソッドは、指定した要素がHashSet内に存在する場合はtrue、存在しない場合はfalseを返します。Contains()メソッドの基本構文public bool Contains (T item);引数には検索対象の要素を渡します。HashSetはハッシュテーブルを基盤としているため、要素の検索はO(1)の計算量で実行でき、非常に高速です。例1:数値型のHashSetでContains()を使う以下は、int型のHashSetに対してContains()メ

  2. C#で配列内に指定した条件に一致する要素が含まれているか確認する方法

    C#で配列の中に、特定の条件に一致する要素が1つでも存在するかどうかを確認したい場合があります。そんなときに便利なのが Array.Exists() メソッドです。このメソッドは、第1引数に検索対象の配列、第2引数に条件を表す述語(Predicate)デリゲートを渡します。たとえば文字列の前方一致を調べたい場合は、StartsWith() メソッドと組み合わせることで簡単に実現できます。以下では、具体的なサンプルコードとその実行結果を通じて、使い方を詳しく見ていきましょう。サンプルコード:StartsWith()で前方一致を判定するusing System; public class Demo