IDictionary<TKey,TValue>.TryGetValue(TKey, TValue) Yöntem

Tanım

Belirtilen anahtarla ilişkili değeri alır.

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

Parametreler

key
TKey

Değeri alınacak anahtar.

value
TValue

Bu yöntem döndürdüğünde, anahtar bulunursa belirtilen anahtarla ilişkili değer; aksi takdirde, parametre türü value için varsayılan değerdir. Bu parametre, başlatmadan iletilir.

Döndürülenler

true uygulayan IDictionary<TKey,TValue> nesne belirtilen anahtara sahip bir öğe içeriyorsa; değilse, false.

Özel durumlar

key, null değeridir.

Örnekler

Örnekte, değerleri almak için yönteminin TryGetValue nasıl kullanılacağı gösterilmektedir. Bir program sözlükte olmayan anahtar değerlerini sık sık denerse, TryGetValue yöntem var olmayan anahtarları almaya çalışırken özel durumlar oluşturan özelliğini (C# dilinde dizin oluşturucu) kullanmaktan Item[] daha verimli olabilir.

Bu kod, derlenip yürütülebilen daha büyük bir örneğin parçasıdır. Bkz. System.Collections.Generic.IDictionary<TKey,TValue>.

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

Açıklamalar

Bu yöntem, yönteminin ve özelliğinin ContainsKey işlevselliğini Item[] birleştirir.

Anahtar bulunamazsa, value parametre türü TValueiçin uygun varsayılan değeri alır; örneğin, tamsayı türleri false için sıfır (0), Boole türleri ve null başvuru türleri için.

Şunlara uygulanır

Ayrıca bkz.