Dictionary.ContainsKey メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > Dictionary.ContainsKey メソッドの意味・解説 

Dictionary.ContainsKey メソッド

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

指定したキーDictionary格納されているかどうか判断します

名前空間: System.Collections.Generic
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Function ContainsKey ( _
    key As TKey _
) As Boolean
Dim instance As Dictionary(Of
 TKey, TValue)
Dim key As TKey
Dim returnValue As Boolean

returnValue = instance.ContainsKey(key)
public bool ContainsKey (
    TKey key
)
public:
virtual bool ContainsKey (
    TKey key
) sealed
public final boolean ContainsKey (
    TKey key
)
public final function ContainsKey (
    key : TKey
) : boolean

パラメータ

key

Dictionaryで検索されるキー

戻り値
指定したキー持つ要素Dictionary格納されている場合trueそれ以外場合false

例外例外
例外種類条件

ArgumentNullException

keynull 参照 (Visual Basic では Nothing) です。

解説解説
使用例使用例

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.");
        }
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からDictionary.ContainsKey メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からDictionary.ContainsKey メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からDictionary.ContainsKey メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Dictionary.ContainsKey メソッド」の関連用語

Dictionary.ContainsKey メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Dictionary.ContainsKey メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS