Übersetzung vorschlagen
 
Andere Vorschläge:

progress indicator
Keine anderen Vorschläge
Per Mausklick bewerten und Feedback geben
MSDN
MSDN Library
.NET Entwicklung
.NET Framework 4
 ContainsKey-Methode
Alle reduzieren/Alle erweitern Alle reduzieren
Inhalt anzeigen:  Englisch mit deutscher ÜbersetzungInhalt anzeigen: Englisch mit deutscher Übersetzung
.NET Framework Class Library
Dictionary<(Of <(TKey, TValue>)>)..::.ContainsKey Method

Determines whether the Dictionary<(Of <(TKey, TValue>)>) contains the specified key.

Namespace:  System.Collections.Generic
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic
Public Function ContainsKey ( _
    key As TKey _
) As Boolean
C#
public bool ContainsKey(
    TKey key
)
Visual C++
public:
virtual bool ContainsKey(
    TKey key
) sealed
F#
abstract ContainsKey : 
        key:'TKey -> bool 
override ContainsKey : 
        key:'TKey -> bool 

Parameters

key
Type: TKey
The key to locate in the Dictionary<(Of <(TKey, TValue>)>).

Return Value

Type: System..::.Boolean
true if the Dictionary<(Of <(TKey, TValue>)>) contains an element with the specified key; otherwise, false.

Implements

IDictionary<(Of <(TKey, TValue>)>)..::.ContainsKey(TKey)
ExceptionCondition
ArgumentNullException

key is nullNothingnullptra null reference (Nothing in Visual Basic).

This method approaches an O(1) operation.

The following code example shows how to use the ContainsKey method to test whether a key exists prior to calling the Add method. 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. Finally, it shows the least efficient way to test whether keys exist, by using the Item property (the indexer in C#).

This code example is part of a larger example provided for the Dictionary<(Of <(TKey, TValue>)>) class.

Visual Basic
' 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.
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 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
C#
// 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"]);
}


...


// 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.");
}


...


// 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.");
}
Visual C++
// 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"]);
}


...


// 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.");
}


...


// 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.");
}

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role not supported), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework-Klassenbibliothek
Dictionary<(Of <(TKey, TValue>)>)..::.ContainsKey-Methode

Bestimmt, ob das Dictionary<(Of <(TKey, TValue>)>) den angegebenen Schlüssel enthält.

Namespace:  System.Collections.Generic
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic
Public Function ContainsKey ( _
    key As TKey _
) As Boolean
C#
public bool ContainsKey(
    TKey key
)
Visual C++
public:
virtual bool ContainsKey(
    TKey key
) sealed
F#
abstract ContainsKey : 
        key:'TKey -> bool 
override ContainsKey : 
        key:'TKey -> bool 

Parameter

key
Typ: TKey
Der im Dictionary<(Of <(TKey, TValue>)>) zu suchende Schlüssel.

Rückgabewert

Typ: System..::.Boolean
true, wenn das Dictionary<(Of <(TKey, TValue>)>) ein Element mit dem angegebenen Schlüssel enthält, andernfalls false.

Implementiert

IDictionary<(Of <(TKey, TValue>)>)..::.ContainsKey(TKey)
AusnahmeBedingung
ArgumentNullException

key hat den Wert nullNothingnullptrNULL-Verweis (Nothing in Visual Basic).

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, das für die Dictionary<(Of <(TKey, TValue>)>)-Klasse bereitgestellt wird.

Visual Basic
' 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.
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 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
C#
// 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"]);
}


...


// 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.");
}


...


// 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.");
}
Visual C++
// 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"]);
}


...


// 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.");
}


...


// 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.");
}

.NET Framework

Unterstützt in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Unterstützt in: 4, 3.5 SP1

Unterstützt in:

Windows 7, Windows Vista SP1 oder höher, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core wird nicht unterstützt), Windows Server 2008 R2 (Server Core wird mit SP1 oder höher unterstützt), Windows Server 2003 SP2

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen für .NET Framework.
Communityinhalt   Was ist Community Content?
Neuen Inhalt hinzufügen RSS  Anmerkungen
Processing
© 2012 Microsoft. Alle Rechte vorbehalten. Nutzungsbedingungen | Markenzeichen | Informationen zur Datensicherheit
Page view tracker