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

定義

取得與指定索引鍵關聯的值。Gets the value associated with the specified key.

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

參數

key
TKey TKey TKey TKey

要取得其值的索引鍵。The key whose value to get.

value
TValue TValue TValue TValue

這個方法傳回時,如果找到索引鍵,則為與指定索引鍵關聯的值,否則為 value 參數的型別預設值。When this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter. 這個參數會以未初始化的狀態傳遞。This parameter is passed uninitialized.

傳回

如果實作 IDictionary<TKey,TValue> 之物件包含具有指定索引鍵的元素,則為 true,否則為 falsetrue if the object that implements IDictionary<TKey,TValue> contains an element with the specified key; otherwise, false.

例外狀況

範例

此範例示範如何使用TryGetValue方法來擷取值。The example shows how to use the TryGetValue method to retrieve values. 如果程式經常會嘗試不在字典中,索引鍵值TryGetValue方法可能會更有效率,比使用Item[TKey]屬性 (C# 索引子),嘗試擷取不存在的索引鍵時擲回例外狀況。If a program frequently tries key values that are not in a dictionary, the TryGetValue method can be more efficient than using the Item[TKey] property (the indexer in C#), which throws exceptions when attempting to retrieve nonexistent keys.

此程式碼是大型的編譯和執行範例的一部分。This code is part of a larger example that can be compiled and executed. 請參閱 System.Collections.Generic.IDictionary<TKey,TValue>See 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

備註

這個方法會結合的功能ContainsKey方法和Item[TKey]屬性。This method combines the functionality of the ContainsKey method and the Item[TKey] property.

如果找不到索引鍵,則value參數的類型取得適當的預設值TValue; 例如,整數類型零 (0)false布林類型,以及null參考型別。If the key is not found, then the value parameter gets the appropriate default value for the type TValue; for example, zero (0) for integer types, false for Boolean types, and null for reference types.

適用於

另請參閱