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

Definition

Hämtar värdet som är associerat med den angivna nyckeln.

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

Parametrar

key
TKey

Nyckeln för det värde som ska hämtas.

value
TValue

När den här metoden returnerar innehåller det värde som är associerat med den angivna nyckeln, om nyckeln hittas. annars standardvärdet för parametertypen value . Den här parametern skickas oinitierad.

Returer

true Dictionary<TKey,TValue> om innehåller ett element med den angivna nyckeln, annars . false

Implementeringar

Undantag

key är null.

Exempel

Exemplet visar hur du använder TryGetValue metoden som ett effektivare sätt att hämta värden i ett program som ofta försöker använda nycklar som inte finns i ordlistan. Exemplet visar däremot också hur Item[] egenskapen (indexeraren i C#) genererar undantag när du försöker hämta obefintliga nycklar.

Det här kodexemplet är en del av ett större exempel för Dictionary<TKey,TValue> klassen (openWith är namnet på ordlistan som används i det här exemplet).

// 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.
match openWith.TryGetValue "tif" with
| true, value -> printfn $"For key = \"tif\", value = {value}."
| _ -> printfn "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
    printfn $"""For key = "tif", value = {openWith["tif"]}"""
with :? KeyNotFoundException ->
    printfn "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

Kommentarer

Den här metoden kombinerar funktionerna i ContainsKey metoden och egenskapen Item[] .

Om nyckeln inte hittas hämtar parametern value lämpligt standardvärde för typen TValue, till exempel 0 (noll) för heltalstyper, false för booleska typer och null för referenstyper.

TryGetValue Använd metoden om koden ofta försöker komma åt nycklar som inte finns i ordlistan. Det är effektivare att använda den här metoden än att fånga den KeyNotFoundException som genereras av Item[] egenskapen.

Den här metoden närmar sig en O(1)-åtgärd.

Gäller för

Se även