IDictionary<TKey,TValue>.ContainsKey(TKey) Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Bepaalt of het IDictionary<TKey,TValue> een element met de opgegeven sleutel bevat.
public:
bool ContainsKey(TKey key);
public bool ContainsKey(TKey key);
abstract member ContainsKey : 'Key -> bool
Public Function ContainsKey (key As TKey) As Boolean
Parameters
- key
- TKey
De sleutel die moet worden gevonden in de IDictionary<TKey,TValue>.
Retouren
trueals het IDictionary<TKey,TValue> element een element met de sleutel bevat; anders. false
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. Het laat ook zien hoe u de TryGetValue methode gebruikt, wat een efficiëntere manier kan zijn om waarden op te halen als een programma vaak sleutelwaarden probeert die zich niet in de woordenlijst bevinden. Ten slotte ziet u hoe u items invoegt met behulp van Item[] de eigenschap (de indexeerfunctie in C#).
Deze code maakt deel uit van een groter voorbeeld dat kan worden gecompileerd en uitgevoerd. Zie 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
Opmerkingen
Implementaties kunnen variëren in hoe ze gelijkheid van objecten bepalen; De klasse gebruikt Comparer<T>.DefaultbijvoorbeeldList<T>, terwijl de Dictionary<TKey,TValue> klasse de gebruiker toestaat de IComparer<T> implementatie op te geven die moet worden gebruikt voor het vergelijken van sleutels.
Implementaties kunnen variëren in of ze toestaan key te zijn null.