Dictionary.ContainsKey メソッド
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As Dictionary(Of TKey, TValue) Dim key As TKey Dim returnValue As Boolean returnValue = instance.ContainsKey(key)
- key
Dictionary 内で検索されるキー。
指定したキーを持つ要素が Dictionary に格納されている場合は true。それ以外の場合は false。



Add メソッドを呼び出す前に、ContainsKey メソッドを使用して、キーが存在するかどうかをテストする方法を次のコード例に示します。また、TryGetValue メソッドを使用して値を取得する方法も示します。これは、プログラムがディクショナリにないキー値を頻繁に試行する場合に、効率的な方法です。最後に、この例では、Item プロパティ (C# ではインデクサ) を使用して、キーが存在するかどうかをテストする最も効率の悪い方法も示します。
このコード例は、Dictionary クラスのトピックで取り上げているコード例の一部分です。
' ContainsKey can be used to test keys before inserting ' them. If Not openWith.ContainsKey("ht") Then openWith.Add("ht", "hypertrm.exe") Console.WriteLine("Value added for key = ""ht"": {0}", _ openWith("ht")) End If <br /><span space="preserve">...</span><br /> ' When a program often has to try keys that turn out not to ' be in the dictionary, TryGetValue can be a more efficient ' way to retrieve values. Dim value As String = "" If openWith.TryGetValue("tif", value) Then Console.WriteLine("For key = ""tif"", value = {0}.", value) Else Console.WriteLine("Key = ""tif"" is not found.") End If <br /><span space="preserve">...</span><br /> ' The default Item property throws an exception if the requested ' key is not in the dictionary. Try Console.WriteLine("For key = ""tif"", value = {0}.", _ openWith("tif")) Catch Console.WriteLine("Key = ""tif"" is not found.") End Try
// ContainsKey can be used to test keys before inserting // them. if (!openWith.ContainsKey("ht")) { openWith.Add("ht", "hypertrm.exe"); Console.WriteLine("Value added for key = \"ht\": {0}", openWith["ht"]); } <br /><span space="preserve">...</span><br /> // When a program often has to try keys that turn out not to // be in the dictionary, TryGetValue can be a more efficient // way to retrieve values. string value = ""; if (openWith.TryGetValue("tif", out value)) { Console.WriteLine("For key = \"tif\", value = {0}.", value); } else { Console.WriteLine("Key = \"tif\" is not found."); } <br /><span space="preserve">...</span><br /> // The indexer throws an exception if the requested key is // not in the dictionary. try { Console.WriteLine("For key = \"tif\", value = {0}.", openWith["tif"]); } catch (KeyNotFoundException) { Console.WriteLine("Key = \"tif\" is not found."); }

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からDictionary.ContainsKey メソッドを検索する場合は、下記のリンクをクリックしてください。

- Dictionary.ContainsKey メソッドのページへのリンク