IDictionary<TKey,TValue>.ContainsKey(TKey) Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Avgör om innehåller IDictionary<TKey,TValue> ett element med den angivna nyckeln.
public:
bool ContainsKey(TKey key);
public bool ContainsKey(TKey key);
abstract member ContainsKey : 'Key -> bool
Public Function ContainsKey (key As TKey) As Boolean
Parametrar
- key
- TKey
Nyckeln som ska hittas IDictionary<TKey,TValue>i .
Returer
true
IDictionary<TKey,TValue> om innehåller ett element med nyckeln, annars . false
Undantag
key är null.
Exempel
I följande kodexempel visas hur du använder ContainsKey metoden för att testa om det finns en nyckel innan metoden anropas Add . Den visar också hur du använder TryGetValue metoden, vilket kan vara ett effektivare sätt att hämta värden om ett program ofta försöker använda nyckelvärden som inte finns i ordlistan. Slutligen visar den hur du infogar objekt med hjälp av Item[] egenskapen (indexeraren i C#).
Den här koden är en del av ett större exempel som kan kompileras och köras. Se även 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
Kommentarer
Implementeringar kan variera i hur de bestämmer objektens likhet. Klassen använder Comparer<T>.Defaulttill exempel List<T> , medan Dictionary<TKey,TValue> klassen tillåter att användaren anger vilken implementering som IComparer<T> ska användas för att jämföra nycklar.
Implementeringarna kan variera beroende på om de tillåter key att de är null.