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

Definizione

Determina se la raccolta Dictionary<TKey,TValue> contiene la chiave specificata.Determines whether the Dictionary<TKey,TValue> contains 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

Parametri

key
TKey TKey TKey TKey

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

Restituisce

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

Implementazioni

Eccezioni

Esempi

Esempio di codice seguente viene illustrato come utilizzare il ContainsKey metodo per verificare se esiste una chiave prima di chiamare il Add (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 per recuperare i valori, ovvero un modo efficiente per recuperare i valori quando un programma tenta di frequente le chiavi che non sono presenti nel dizionario.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. Infine, Mostra il modo meno efficiente per testare se le chiavi esistono, utilizzando il Item[TKey] proprietà (l'indicizzatore in c#).Finally, it shows the least efficient way to test whether keys exist, by using the Item[TKey] property (the indexer in C#).

Questo esempio di codice è parte di un esempio più esaustivo disponibile per il Dictionary<TKey,TValue> classe (openWith è il nome del dizionario usato in questo esempio).This code example is part of a larger example provided for the Dictionary<TKey,TValue> class (openWith is the name of the Dictionary used in this example).

// 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

Questo metodo si avvicina un'operazione o (1).This method approaches an O(1) operation.

Si applica a

Vedi anche