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,为引用类型获取 nullIf 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.

适用于