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

Definizione

Determina se IDictionary<TKey,TValue> contiene un elemento con la chiave specificata.Determines whether the IDictionary<TKey,TValue> contains an element with the specified key.

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

Parametri

key
TKey TKey TKey TKey

Chiave da individuare in IDictionary<TKey,TValue>.The key to locate in the IDictionary<TKey,TValue>.

Restituisce

true se IDictionary<TKey,TValue> contiene un elemento con la chiave; in caso contrario, false.true if the IDictionary<TKey,TValue> contains an element with the key; otherwise, false.

Eccezioni

Esempi

Nell'esempio di codice seguente viene illustrato come utilizzare ContainsKey il metodo per verificare se una chiave esiste prima di chiamare Add il metodo.The following code example shows how to use the ContainsKey method to test whether a key exists prior to calling the Add method. Viene inoltre illustrato come utilizzare il TryGetValue metodo, che può essere un modo più efficiente per recuperare i valori se un programma tenta spesso valori di chiave non presenti nel dizionario.It also shows how to use the TryGetValue method, which can be a more efficient way to retrieve values if a program frequently tries key values that are not in the dictionary. Infine, viene illustrato come inserire elementi utilizzando Item[TKey] la proprietà (l'indicizzatore in C#).Finally, it shows how to insert items using Item[TKey] property (the indexer in C#).

Questo codice fa parte di un esempio più ampio che può essere compilato ed eseguito.This code is part of a larger example that can be compiled and executed. Vedere System.Collections.Generic.IDictionary<TKey,TValue>.See 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 (!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

Commenti

Le implementazioni possono variare in modo da determinare l'uguaglianza di oggetti; ad esempio, la List<T> classe utilizza Comparer<T>.Default, mentre la Dictionary<TKey,TValue> classe consente all'utente di specificare l' IComparer<T> implementazione da utilizzare per il confronto delle chiavi.Implementations can vary in how they determine equality of objects; for example, the List<T> class uses Comparer<T>.Default, whereas the Dictionary<TKey,TValue> class allows the user to specify the IComparer<T> implementation to use for comparing keys.

Le implementazioni possono variare a seconda che key consentano di essere null.Implementations can vary in whether they allow key to be null.

Si applica a