Share via


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

Definisi

Mendapatkan nilai yang terkait dengan kunci yang ditentukan.

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

Parameter

key
TKey

Kunci nilai yang akan didapatkan.

value
TValue

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

Mengembalikan

trueDictionary<TKey,TValue> jika berisi elemen dengan kunci yang ditentukan; jika tidak, false.

Penerapan

Pengecualian

keyadalah null.

Contoh

Contoh menunjukkan cara menggunakan TryGetValue metode sebagai cara yang lebih efisien untuk mengambil nilai dalam program yang sering mencoba kunci yang tidak ada dalam kamus. Sebaliknya, contohnya juga menunjukkan bagaimana Item[] properti (pengindeks di C#) melemparkan pengecualian saat mencoba mengambil kunci yang tidak ada.

Contoh kode ini adalah bagian dari contoh yang lebih besar yang disediakan untuk Dictionary<TKey,TValue> kelas (openWith adalah nama Kamus yang digunakan dalam contoh ini).

// 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, 0 (nol) untuk jenis bilangan bulat, false untuk jenis Boolean, dan null untuk jenis referensi.

TryGetValue Gunakan metode jika kode Anda sering mencoba mengakses kunci yang tidak ada dalam kamus. Menggunakan metode ini lebih efisien daripada menangkap yang dilemparkan KeyNotFoundExceptionItem[] oleh properti .

Metode ini mendekati operasi O(1).

Berlaku untuk

Lihat juga