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

Definition

Bestimmt, ob das Dictionary<TKey,TValue> den angegebenen Schlüssel enthält.Determines whether the Dictionary<TKey,TValue> contains the specified key.

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

Parameter

key
TKey TKey TKey TKey

Der im Dictionary<TKey,TValue> zu suchende Schlüssel.The key to locate in the Dictionary<TKey,TValue>.

Gibt zurück

true, wenn das Dictionary<TKey,TValue> ein Element mit dem angegebenen Schlüssel enthält, andernfalls false.true if the Dictionary<TKey,TValue> contains an element with the specified key; otherwise, false.

Implementiert

Ausnahmen

Beispiele

Im folgenden Codebeispiel wird veranschaulicht, wie Sie mit der ContainsKey Methode zum Überprüfen, ob ein Schlüssel vorhanden, vor dem Aufrufen ist der Add Methode.The following code example shows how to use the ContainsKey method to test whether a key exists prior to calling the Add method. Darüber hinaus wird mit der TryGetValue Methode zum Abrufen von Werten, die ist eine effiziente Möglichkeit zum Abrufen von Werten, wenn ein Programm häufig Schlüssel versucht, die nicht im Wörterbuch vorhanden sind.It also shows how to use the TryGetValue method to retrieve values, which is an efficient way to retrieve values when a program frequently tries keys that are not in the dictionary. Schließlich werden der am wenigsten effizienten Methode zum Testen, ob ein Schlüssel vorhanden sind, mithilfe der Item[TKey] -Eigenschaft (Indexer in c#).Finally, it shows the least efficient way to test whether keys exist, by using the Item[TKey] property (the indexer in C#).

Dieses Codebeispiel ist Teil eines umfangreicheren Beispiels für die Dictionary<TKey,TValue> Klasse (openWith ist der Name des Wörterbuchs in diesem Beispiel verwendet).This code example is part of a larger example provided for the Dictionary<TKey,TValue> class (openWith is the name of the Dictionary used in this example).

// 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

Hinweise

Diese Methode o(1)-Vorgang ein.This method approaches an O(1) operation.

Gilt für:

Siehe auch