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

Definition

Bestimmt, ob das IDictionary<TKey,TValue> Element mit dem angegebenen Schlüssel enthält.

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

Parameter

key
TKey

Der Schlüssel, der in der IDictionary<TKey,TValue>Datei gefunden werden soll.

Gibt zurück

true wenn das IDictionary<TKey,TValue> Element mit dem Schlüssel enthält; andernfalls false.

Ausnahmen

key ist null.

Beispiele

Im folgenden Codebeispiel wird gezeigt, wie Sie die ContainsKey Methode verwenden, um zu testen, ob ein Schlüssel vorhanden ist, bevor die Add Methode aufgerufen wird. Außerdem wird gezeigt, wie Sie die TryGetValue Methode verwenden, was eine effizientere Möglichkeit zum Abrufen von Werten sein kann, wenn ein Programm häufig Schlüsselwerte versucht, die sich nicht im Wörterbuch befinden. Schließlich wird gezeigt, wie Elemente mithilfe der Item[] Eigenschaft eingefügt werden (der Indexer in C#).

Dieser Code ist Teil eines größeren Beispiels, das kompiliert und ausgeführt werden kann. Siehe 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 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", 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 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

Implementierungen können variieren, wie sie die Gleichheit von Objekten bestimmen; Die Klasse verwendet Comparer<T>.DefaultzList<T>. B. die Klasse, während der Dictionary<TKey,TValue> Benutzer die Implementierung angeben kann, die IComparer<T> zum Vergleichen von Schlüsseln verwendet werden soll.

Implementierungen können variieren, ob sie zulassen keynull.

Gilt für: