IReadOnlyDictionary<TKey,TValue>.TryGetValue(TKey, TValue) 方法

定義

取得與指定之索引鍵相關聯的值。Gets the value that is 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 * 'Value -> bool
Public Function TryGetValue (key As TKey, ByRef value As TValue) As Boolean

參數

key
TKey

要尋找的索引鍵。The key to locate.

value
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.

傳回

如果實作 IReadOnlyDictionary<TKey,TValue> 介面之物件包含具有指定之索引鍵的項目,則為 true,否則為 falsetrue if the object that implements the IReadOnlyDictionary<TKey,TValue> interface contains an element that has the specified key; otherwise, false.

例外狀況

keynullkey is null.

備註

這個方法結合了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, the value parameter gets the appropriate default value for the type TValue: for example, 0 (zero) for integer types, false for Boolean types, and null for reference types.

適用於