Hinweis: Diese Methode ist neu in .NET Framework, Version 2.0.
Bestimmt, ob das
Dictionary den angegebenen Schlüssel enthält.
Namespace: System.Collections.Generic
Assembly: mscorlib (in mscorlib.dll)
Visual Basic (Deklaration)
Public Function ContainsKey ( _
key As TKey _
) As Boolean
Visual Basic (Verwendung)
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
Parameter
- key
Der im Dictionary zu suchende Schlüssel.
Rückgabewert
true, wenn das Dictionary ein Element mit dem angegebenen Schlüssel enthält, andernfalls false.
Diese Methode kommt einem O(1)-Vorgang nahe.
Im folgenden Codebeispiel wird veranschaulicht, wie vor dem Aufrufen der Add-Methode mithilfe der ContainsKey-Methode überprüft wird, ob ein Schlüssel vorhanden ist. Außerdem wird das Abrufen von Werten mithilfe der TryGetValue-Methode veranschaulicht. Wenn ein Programm häufig versucht, auf Werte zuzugreifen, die nicht im Wörterbuch enthalten sind, lassen sich Werte auf diese Weise effizienter abrufen. Abschließend wird mit der Item-Eigenschaft (Indexer in C#) die am wenigsten effiziente Methode zum Testen des Vorhandenseins von Schlüsseln gezeigt.
Dieses Codebeispiel ist Teil eines umfangreicheren Beispiels für die Dictionary-Klasse.
' 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.");
}
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
.NET Framework
Unterstützt in: 2.0
.NET Compact Framework
Unterstützt in: 2.0