Dictionary<TKey,TValue>.TryGetValue(TKey, TValue) Metodo

Definizione

Ottiene il valore associato alla chiave specificata.

public:
 virtual bool TryGetValue(TKey key, [Runtime::InteropServices::Out] TValue % value);
public bool TryGetValue (TKey key, out TValue value);
abstract member TryGetValue : 'Key * 'Value -> bool
override this.TryGetValue : 'Key * 'Value -> bool
Public Function TryGetValue (key As TKey, ByRef value As TValue) As Boolean

Parametri

key
TKey

Chiave del valore da ottenere.

value
TValue

Quando termina, questo metodo restituisce il valore associato alla chiave specificata nel caso in cui la chiave venga trovata; in caso contrario restituisce il valore predefinito per il tipo di parametro value. Questo parametro viene passato non inizializzato.

Restituisce

Boolean

true se Dictionary<TKey,TValue> contiene un elemento con la chiave specificata; in caso contrario, false.

Implementazioni

Eccezioni

key è null.

Esempio

Nell'esempio viene illustrato come usare il TryGetValue metodo come modo più efficiente per recuperare i valori in un programma che prova spesso le chiavi che non si trovano nel dizionario. Al contrario, l'esempio mostra anche come la Item[] proprietà (indicizzatore in C#) genera eccezioni quando si tenta di recuperare chiavi inesistente.

Questo esempio di codice fa parte di un esempio più grande fornito per la Dictionary<TKey,TValue> classe (openWith è il nome del dizionario usato in questo esempio).

// 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 combina la funzionalità del ContainsKey metodo e la Item[] proprietà.

Se la chiave non viene trovata, il parametro value ottiene il valore predefinito per il tipo TValue; ad esempio 0 (zero) per i tipi Integer, false per i tipi Boolean e null per i tipi di riferimento.

Usare il metodo se il TryGetValue codice tenta spesso di accedere alle chiavi non presenti nel dizionario. L'uso di questo metodo è più efficiente di intercettare l'oggetto KeyNotFoundException Item[] generato dalla proprietà.

Questo metodo si avvicina a un'operazione O(1).

Si applica a

Vedi anche