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

Definitie

Bepaalt of het SortedDictionary<TKey,TValue> een element met de opgegeven sleutel bevat.

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

Parameters

key
TKey

De sleutel die moet worden gevonden in de SortedDictionary<TKey,TValue>.

Retouren

trueals het SortedDictionary<TKey,TValue> een element met de opgegeven sleutel bevat; anders. false

Implementeringen

Uitzonderingen

key is null.

Voorbeelden

In het volgende codevoorbeeld ziet u hoe u de ContainsKey methode gebruikt om te testen of er een sleutel bestaat voordat u de Add methode aanroept. Ook ziet u hoe u de TryGetValue methode gebruikt om waarden op te halen. Dit is een efficiënte manier om waarden op te halen wanneer een programma vaak sleutels probeert die zich niet in de woordenlijst bevinden. Ten slotte ziet u de minst efficiënte manier om te testen of er sleutels bestaan met behulp van de Item[] eigenschap (de indexeerfunctie in C#).

Dit codevoorbeeld maakt deel uit van een groter voorbeeld voor de SortedDictionary<TKey,TValue> klasse.

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

Opmerkingen

Deze methode is een O(logboek n) bewerking.

Van toepassing op

Zie ook