SortedDictionary<TKey,TValue>.ContainsKey(TKey) Metoda
Definicja
Określa, czy SortedDictionary<TKey,TValue> zawiera element z określonym kluczem.Determines whether the SortedDictionary<TKey,TValue> contains an element with the specified key.
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
Parametry
- key
- TKey
Klucz, który ma zostać zlokalizowany w SortedDictionary<TKey,TValue> .The key to locate in the SortedDictionary<TKey,TValue>.
Zwraca
true
Jeśli SortedDictionary<TKey,TValue> zawiera element z określonym kluczem; w przeciwnym razie, false
.true
if the SortedDictionary<TKey,TValue> contains an element with the specified key; otherwise, false
.
Implementuje
Wyjątki
key
to null
.key
is null
.
Przykłady
Poniższy przykład kodu pokazuje, jak używać ContainsKey metody do testowania, czy klucz istnieje przed wywołaniem Add metody.The following code example shows how to use the ContainsKey method to test whether a key exists prior to calling the Add method. Pokazano również, jak używać TryGetValue metody do pobierania wartości, co jest wydajnym sposobem pobierania wartości, gdy program często próbuje klucze, które nie znajdują się w słowniku.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. Na koniec pokazuje najmniej wydajny sposób, aby sprawdzić, czy klucze istnieją, przy użyciu Item[] właściwości (indeksator w języku C#).Finally, it shows the least efficient way to test whether keys exist, by using the Item[] property (the indexer in C#).
Ten przykład kodu jest częścią większego przykładu dostarczonego dla SortedDictionary<TKey,TValue> klasy.This code example is part of a larger example provided for the SortedDictionary<TKey,TValue> class.
// 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
Uwagi
Ta metoda jest operacją O (log n
).This method is an O(log n
) operation.