IDictionary<TKey,TValue>.ContainsKey(TKey) Metoda

Definicja

Określa, czy element IDictionary<TKey,TValue> zawiera element z określonym kluczem.

public:
 bool ContainsKey(TKey key);
public bool ContainsKey (TKey key);
abstract member ContainsKey : 'Key -> bool
Public Function ContainsKey (key As TKey) As Boolean

Parametry

key
TKey

Klucz do zlokalizowania w pliku IDictionary<TKey,TValue>.

Zwraca

true jeśli element IDictionary<TKey,TValue> zawiera element z kluczem; w przeciwnym razie false.

Wyjątki

key to null.

Przykłady

W poniższym przykładzie kodu pokazano, jak używać ContainsKey metody do testowania, czy klucz istnieje przed wywołaniem Add metody. Pokazuje również, jak używać TryGetValue metody , która może być bardziej wydajnym sposobem pobierania wartości, jeśli program często próbuje wartości kluczy, które nie znajdują się w słowniku. Na koniec pokazano, jak wstawić elementy przy użyciu Item[] właściwości (indeksator w języku C#).

Ten kod jest częścią większego przykładu, który można skompilować i wykonać. Zobacz: .

// 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 (!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", 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.
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 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

Implementacje mogą się różnić w sposobie określania równości obiektów; na przykład List<T> klasa używa Comparer<T>.Defaultklasy , natomiast Dictionary<TKey,TValue> klasa umożliwia użytkownikowi określenie IComparer<T> implementacji do użycia do porównywania kluczy.

Implementacje mogą się różnić w zależności od tego, czy zezwalają key na wartość null.

Dotyczy