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>類別會使用Comparer<T>.Default,而Dictionary<TKey,TValue>類別可讓使用者指定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.

實作有所不同是否允許keynullImplementations can vary in whether they allow key to be null.

適用於