C#のDictionary.ContainsKey()メソッドの使い方を徹底解説!サンプルコード付き
C#のDictionary.ContainsKey()メソッドは、Dictionary<TKey, TValue>内に指定したキーが存在するかどうかを判定するためのメソッドです。キーが見つかればtrue、見つからなければfalseを返します。
構文
public bool ContainsKey (TKey key);
パラメータ「key」には、検索対象となるキーを指定します。
使用例1:キーが存在する場合
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Dictionary<string, string> dict =
new Dictionary<string, string>();
dict.Add("One", "John");
dict.Add("Two", "Tom");
dict.Add("Three", "Jacob");
dict.Add("Four", "Kevin");
dict.Add("Five", "Nathan");
Console.WriteLine("要素数 = " + dict.Count);
Console.WriteLine("\nキーと値のペア...");
foreach(KeyValuePair<string, string> res in dict){
Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
}
if (dict.ContainsKey("Three"))
Console.WriteLine("キーが見つかりました!");
else
Console.WriteLine("キーは辞書内に存在しません!");
dict.Clear();
Console.WriteLine("クリア後のキーと値のペア...");
foreach(KeyValuePair<string, string> res in dict){
Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
}
Console.WriteLine("現在の要素数 = " + dict.Count);
}
}出力結果
要素数 = 5 キーと値のペア... Key = One, Value = John Key = Two, Value = Tom Key = Three, Value = Jacob Key = Four, Value = Kevin Key = Five, Value = Nathan キーが見つかりました! クリア後のキーと値のペア... 現在の要素数 = 0
この例では、キー「Three」が辞書に登録されているため、「キーが見つかりました!」と表示されます。その後、Clear()メソッドですべての要素を削除すると、要素数が0になっていることを確認できます。
使用例2:キーが存在しない場合
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Dictionary<string, string> dict =
new Dictionary<string, string>();
dict.Add("One", "John");
dict.Add("Two", "Tom");
dict.Add("Three", "Jacob");
dict.Add("Four", "Kevin");
dict.Add("Five", "Nathan");
Console.WriteLine("要素数 = " + dict.Count);
Console.WriteLine("\nキーと値のペア...");
foreach(KeyValuePair<string, string> res in dict){
Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
}
if (dict.ContainsKey("mykey"))
Console.WriteLine("キーが見つかりました!");
else
Console.WriteLine("キーは辞書内に存在しません!");
dict.Clear();
Console.WriteLine("クリア後のキーと値のペア...");
foreach(KeyValuePair<string, string> res in dict){
Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
}
Console.WriteLine("現在の要素数 = " + dict.Count);
}
}出力結果
要素数 = 5 キーと値のペア... Key = One, Value = John Key = Two, Value = Tom Key = Three, Value = Jacob Key = Four, Value = Kevin Key = Five, Value = Nathan キーは辞書内に存在しません! クリア後のキーと値のペア... 現在の要素数 = 0
この例では、キー「mykey」は辞書に登録されていないため、「キーは辞書内に存在しません!」と表示されます。
まとめ
ContainsKey()メソッドを使用することで、Dictionary内のキーの有無を簡単に確認できます。存在しないキーに直接アクセスすると例外が発生するため、値を取得する前にこのメソッドでチェックしておくと安全です。また、TryGetValue()メソッドを使えば、キーの存在確認と値の取得を一度に行うことも可能です。
-
Node.jsのcrypto.generateKeyPair()メソッドで非対称鍵ペアを生成する方法
crypto.generateKeyPair()メソッドは、指定した種類の新しい非対称鍵ペア(公開鍵と秘密鍵のペア)を生成するために使用されます。鍵ペアの生成に対応しているタイプは、RSA、DSA、EC、Ed25519、Ed448、X25519、X448、DHです。publicKeyEncodingまたはprivateKeyEncodingが指定された場合、この関数は結果に対してkeyObject.export()を呼び出した場合と同じように動作し、エンコードされた鍵データを返します。指定されていない場合は、keyObjectの該当する部分がそのまま返されます。 構文 crypto.gene
-
HTML DOM Storage key()メソッドの使い方を徹底解説
HTML DOM Storage key()メソッドとはHTML DOMのStorage key()メソッドは、ストレージオブジェクト内の指定したインデックス位置に保存されているキー名を取得するためのメソッドです。取得したいキーの番号(インデックス)は、引数としてkey()メソッドに渡します。このメソッドで利用できるストレージオブジェクトは、sessionStorageオブジェクトとlocalStorageオブジェクトの2種類です。構文Storage key()メソッドの基本構文は以下のとおりです。localStorageの場合localStorage.key(index);sessionSt