Dictionary<TKey,TValue>.TryGetValue(TKey, TValue) Yöntem
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
Belirtilen anahtarla ilişkili değeri alır.
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
Parametreler
- key
- TKey
Alınacak değerin anahtarı.
- value
- TValue
Bu yöntem döndürdüğünde, anahtar bulunursa belirtilen anahtarla ilişkili değeri içerir; aksi takdirde, parametresinin türü için varsayılan değerdir value . Bu parametre başlatılmamış olarak geçirilir.
Döndürülenler
true belirtilen Dictionary<TKey,TValue> anahtara sahip bir öğe içeriyorsa; değilse, false.
Uygulamalar
Özel durumlar
key, null'e eşittir.
Örnekler
Örnek, sözlükte TryGetValue olmayan anahtarları sık sık deneyen bir programdaki değerleri almak için yönteminin nasıl daha verimli bir yol olarak kullanılacağını gösterir. Buna karşılık, örnekte ayrıca özelliğin Item[] (C#'deki dizin oluşturucu) var olmayan anahtarları almaya çalışırken nasıl özel durumlar oluşturacakları da gösterilir.
Bu kod örneği, sınıfı için Dictionary<TKey,TValue> sağlanan daha büyük bir örneğin parçasıdır (openWith bu örnekte kullanılan Sözlüğün adıdır).
// 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
Açıklamalar
Bu yöntem, yönteminin ContainsKey işlevselliğini ve Item[] özelliğini 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 0 (sıfır), Boole türleri ve null başvuru türleri için.
TryGetValue Kodunuz sık sık sözlükte olmayan anahtarlara erişmeye çalışırsa yöntemini kullanın. Bu yöntemin kullanılması, özelliği tarafından KeyNotFoundException atılan öğesini Item[] yakalamaktan daha verimlidir.
Bu yöntem bir O(1) işlemine yaklaşır.