IDictionary<TKey,TValue>.ContainsKey(TKey) IDictionary<TKey,TValue>.ContainsKey(TKey) IDictionary<TKey,TValue>.ContainsKey(TKey) IDictionary<TKey,TValue>.ContainsKey(TKey) Method

定義

指定したキーの要素が IDictionary<TKey,TValue> に格納されているかどうかを確認します。Determines whether the IDictionary<TKey,TValue> contains an element with the specified key.

public:
 bool ContainsKey(TKey key);
public bool ContainsKey (TKey key);
abstract member ContainsKey : 'Key -> bool
Public Function ContainsKey (key As TKey) As Boolean

パラメーター

key
TKey TKey TKey TKey

IDictionary<TKey,TValue> 内で検索されるキー。The key to locate in the IDictionary<TKey,TValue>.

戻り値

指定したキーを持つ要素を true が保持している場合は IDictionary<TKey,TValue>。それ以外の場合は falsetrue if the IDictionary<TKey,TValue> contains an element with the key; otherwise, false.

例外

メソッドを使用ContainsKeyして、 Addメソッドを呼び出す前にキーが存在するかどうかをテストする方法を次のコード例に示します。The following code example shows how to use the ContainsKey method to test whether a key exists prior to calling the Add method. また、メソッドのTryGetValue使用方法も示しています。これは、プログラムがディクショナリに含まれていないキー値を頻繁に試行する場合に、より効率的に値を取得する方法です。It also shows how to use the TryGetValue method, which can be a more efficient way to retrieve values if a program frequently tries key values that are not in the dictionary. 最後に、プロパティ (のItem[TKey] C#インデクサー) を使用して項目を挿入する方法を示します。Finally, it shows how to insert items using Item[TKey] property (the indexer in C#).

このコードは、コンパイルして実行できる大きな例の一部です。This code is part of a larger example that can be compiled and executed. 以下を参照してください。System.Collections.Generic.IDictionary<TKey,TValue>See System.Collections.Generic.IDictionary<TKey,TValue>.

// 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"]);
}
// 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"]);
}
' 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
// 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", value))
{
    Console::WriteLine("For key = \"tif\", value = {0}.", value);
}
else
{
    Console::WriteLine("Key = \"tif\" is not found.");
}
// 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.");
}
' 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
// 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.");
}
// 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.");
}
' 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

注釈

実装は、オブジェクトが等しいかどうかを判断する方法によって異なります。たとえばList<T> 、クラスはをDictionary<TKey,TValue>使用Comparer<T>.Defaultしますが、クラスでは、キーのIComparer<T>比較に使用する実装をユーザーが指定できます。Implementations can vary in how they determine equality of objects; for example, the List<T> class uses Comparer<T>.Default, whereas the Dictionary<TKey,TValue> class allows the user to specify the IComparer<T> implementation to use for comparing keys.

実装は、にすることがkey許可さnullれているかどうかによって異なります。Implementations can vary in whether they allow key to be null.

適用対象