IDictionary<TKey,TValue>.TryGetValue(TKey, TValue) Metode

Definisi

Mendapatkan nilai yang terkait dengan kunci yang ditentukan.

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

Parameter

key
TKey

Kunci yang nilainya akan didapatkan.

value
TValue

Ketika metode ini kembali, nilai yang terkait dengan kunci yang ditentukan, jika kunci ditemukan; jika tidak, nilai default untuk jenis value parameter. Parameter ini diteruskan tanpa diinisialisasi.

Mengembalikan

true jika objek yang mengimplementasikan IDictionary<TKey,TValue> berisi elemen dengan kunci yang ditentukan; jika tidak, false.

Pengecualian

keyadalah null.

Contoh

Contoh menunjukkan cara menggunakan TryGetValue metode untuk mengambil nilai. Jika program sering mencoba nilai kunci yang tidak ada dalam kamus, TryGetValue metode ini bisa lebih efisien daripada menggunakan Item[] properti (pengindeks di C#), yang melemparkan pengecualian saat mencoba mengambil kunci yang tidak ada.

Kode ini adalah bagian dari contoh yang lebih besar yang dapat dikompilasi dan dijalankan. Lihat 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

Keterangan

Metode ini menggabungkan fungsionalitas ContainsKey metode dan Item[] properti .

Jika kunci tidak ditemukan, maka value parameter mendapatkan nilai default yang sesuai untuk jenis TValue; misalnya, nol (0) untuk jenis bilangan bulat, false untuk jenis Boolean, dan null untuk jenis referensi.

Berlaku untuk

Lihat juga