CollectionExtensions.GetValueOrDefault 方法
定义
重载
GetValueOrDefault<TKey,TValue>(IReadOnlyDictionary<TKey,TValue>, TKey) |
尝试获取与 |
GetValueOrDefault<TKey,TValue>(IReadOnlyDictionary<TKey,TValue>, TKey, TValue) |
尝试获取 |
GetValueOrDefault<TKey,TValue>(IReadOnlyDictionary<TKey,TValue>, TKey)
尝试获取与 dictionary
中的指定 key
相关联的值。Tries to get the value associated with the specified key
in the dictionary
.
public:
generic <typename TKey, typename TValue>
[System::Runtime::CompilerServices::Extension]
static TValue GetValueOrDefault(System::Collections::Generic::IReadOnlyDictionary<TKey, TValue> ^ dictionary, TKey key);
public static TValue GetValueOrDefault<TKey,TValue> (this System.Collections.Generic.IReadOnlyDictionary<TKey,TValue> dictionary, TKey key);
static member GetValueOrDefault : System.Collections.Generic.IReadOnlyDictionary<'Key, 'Value> * 'Key -> 'Value
<Extension()>
Public Function GetValueOrDefault(Of TKey, TValue) (dictionary As IReadOnlyDictionary(Of TKey, TValue), key As TKey) As TValue
类型参数
- TKey
字典中的键的类型。The type of the keys in the dictionary.
- TValue
字典中的值的类型。The type of the values in the dictionary.
参数
- dictionary
- IReadOnlyDictionary<TKey,TValue>
一个字典,其键类型为 TKey
,其值类型为 TValue
。A dictionary with keys of type TKey
and values of type TValue
.
- key
- TKey
要获取的值的键。The key of the value to get.
返回
一个 TValue
实例。A TValue
instance. 如果方法成功,则返回的对象是与指定的 key
相关联的值。When the method is successful, the returned object is the value associated with the specified key
. 如果方法失败,则返回 TValue
的 default
值。When the method fails, it returns the default
value for TValue
.
异常
dictionary
为 null
。dictionary
is null
.
GetValueOrDefault<TKey,TValue>(IReadOnlyDictionary<TKey,TValue>, TKey, TValue)
尝试获取 dictionary
中与指定键相关联的值。Tries to get the value associated with the specified key in the dictionary
.
public:
generic <typename TKey, typename TValue>
[System::Runtime::CompilerServices::Extension]
static TValue GetValueOrDefault(System::Collections::Generic::IReadOnlyDictionary<TKey, TValue> ^ dictionary, TKey key, TValue defaultValue);
public static TValue GetValueOrDefault<TKey,TValue> (this System.Collections.Generic.IReadOnlyDictionary<TKey,TValue> dictionary, TKey key, TValue defaultValue);
static member GetValueOrDefault : System.Collections.Generic.IReadOnlyDictionary<'Key, 'Value> * 'Key * 'Value -> 'Value
<Extension()>
Public Function GetValueOrDefault(Of TKey, TValue) (dictionary As IReadOnlyDictionary(Of TKey, TValue), key As TKey, defaultValue As TValue) As TValue
类型参数
- TKey
字典中的键的类型。The type of the keys in the dictionary.
- TValue
字典中的值的类型。The type of the values in the dictionary.
参数
- dictionary
- IReadOnlyDictionary<TKey,TValue>
一个字典,其键类型为 TKey
,其值类型为 TValue
。A dictionary with keys of type TKey
and values of type TValue
.
- key
- TKey
要获取的值的键。The key of the value to get.
- defaultValue
- TValue
dictionary
找不到与指定的 key
关联的值时要返回的默认值。The default value to return when the dictionary
cannot find a value associated with the specified key
.
返回
一个 TValue
实例。A TValue
instance. 如果方法成功,则返回的对象是与指定的 key
相关联的值。When the method is successful, the returned object is the value associated with the specified key
. 如果方法失败,则返回 defaultValue
。When the method fails, it returns defaultValue
.
异常
dictionary
为 null
。dictionary
is null
.