SortedList<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 di cui si deve ottenere il valore.

value
TValue

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

Restituisce

true se SortedList<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 nell'elenco ordinato. 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 SortedList<TKey,TValue> classe.

// When a program often has to try keys that turn out not to
// be in the list, 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 list, 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 list, 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
// When a program often has to try keys that turn out not to
// be in the list, TryGetValue can be a more efficient
// way to retrieve values.
match openWith.TryGetValue("tif") with
| true, value ->
    printfn "For key = \"tif\", value = {value}."
| false, _ ->
    printfn "Key = \"tif\" is not found."
// The indexer throws an exception if the requested key is
// not in the list.
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 list.
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 list.
Try
    Console.WriteLine("For key = ""tif"", value = {0}.", _
        openWith("tif"))
Catch 
    Console.WriteLine("Key = ""tif"" is not found.")
End Try
// The indexer throws an exception if the requested key is
// not in the list.
try
    printfn $"""For key = "tif", value = {openWith["tif"]}."""
with 
    | :? KeyNotFoundException ->
        printfn "Key = \"tif\" is not found."

Commenti

Questo metodo combina la funzionalità del ContainsKey metodo e la Item[] proprietà.

Se la chiave non viene trovata, il parametro ottiene il value valore predefinito appropriato per il tipo TValuedi valore , ad esempio zero (0) per i tipi integer, false per i tipi booleani e null per i tipi di riferimento.

Questo metodo esegue una ricerca binaria; pertanto, questo metodo è un'operazione O(log n), dove n è Count.

Si applica a

Vedi anche