SortedList<TKey,TValue>.TryGetValue(TKey, TValue) Metoda

Definicja

Pobiera wartość skojarzoną z określonym kluczem.

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

Parametry

key
TKey

Klucz, którego wartość ma być pobierana.

value
TValue

Gdy ta metoda zwróci wartość skojarzona z określonym kluczem, jeśli zostanie znaleziony klucz; w przeciwnym razie wartość domyślna typu parametru value . Ten parametr jest przekazywany jako niezainicjowany.

Zwraca

Boolean

true jeśli element SortedList<TKey,TValue> zawiera element z określonym kluczem; w przeciwnym razie false.

Implementuje

Wyjątki

key to null.

Przykłady

W przykładzie pokazano, jak używać TryGetValue metody jako bardziej wydajnego sposobu pobierania wartości w programie, który często próbuje używać kluczy, które nie znajdują się na posortowanej liście. Z kolei w przykładzie pokazano również, jak Item[] właściwość (indeksator w języku C#) zgłasza wyjątki podczas próby pobrania nieistniejących kluczy.

Ten przykład kodu jest częścią większego przykładu udostępnionego SortedList<TKey,TValue> dla klasy .

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

Uwagi

Ta metoda łączy funkcje ContainsKey metody i Item[] właściwości .

Jeśli klucz nie zostanie znaleziony, value parametr pobiera odpowiednią wartość domyślną dla typu TValuewartości, na przykład zero (0) dla typów całkowitych, false dla typów logicznych i null dla typów referencyjnych.

Ta metoda wykonuje wyszukiwanie binarne; w związku z tym ta metoda jest operacją O(log n), gdzie n to Count.

Dotyczy

Zobacz też