CollectionExtensions.GetValueOrDefault 方法

定義

多載

GetValueOrDefault<TKey,TValue>(IReadOnlyDictionary<TKey,TValue>, TKey)

嘗試取得與 dictionary 中所指定 key 建立關聯的值。

GetValueOrDefault<TKey,TValue>(IReadOnlyDictionary<TKey,TValue>, TKey, TValue)

嘗試取得與 dictionary 中所指定 key 建立關聯的值。

GetValueOrDefault<TKey,TValue>(IReadOnlyDictionary<TKey,TValue>, TKey)

來源:
CollectionExtensions.cs
來源:
CollectionExtensions.cs
來源:
CollectionExtensions.cs

嘗試取得與 dictionary 中所指定 key 建立關聯的值。

C#
public static TValue? GetValueOrDefault<TKey,TValue> (this System.Collections.Generic.IReadOnlyDictionary<TKey,TValue> dictionary, TKey key);
C#
public static TValue GetValueOrDefault<TKey,TValue> (this System.Collections.Generic.IReadOnlyDictionary<TKey,TValue> dictionary, TKey key);

類型參數

TKey

字典中的索引鍵類型。

TValue

字典中的值類型。

參數

dictionary
IReadOnlyDictionary<TKey,TValue>

具有 TKey 類型索引鍵和 TValue 類型值的字典。

key
TKey

要取得之值的索引鍵。

傳回

TValue

TValue 執行個體。 當方法成功時,傳回之物件會是與所指定 key 建立關聯的值。 當方法失敗時,會傳回 TValuedefault 值。

例外狀況

dictionarynull

適用於

.NET 9 和其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

GetValueOrDefault<TKey,TValue>(IReadOnlyDictionary<TKey,TValue>, TKey, TValue)

來源:
CollectionExtensions.cs
來源:
CollectionExtensions.cs
來源:
CollectionExtensions.cs

嘗試取得與 dictionary 中所指定 key 建立關聯的值。

C#
public static TValue GetValueOrDefault<TKey,TValue> (this System.Collections.Generic.IReadOnlyDictionary<TKey,TValue> dictionary, TKey key, TValue defaultValue);

類型參數

TKey

字典中的索引鍵類型。

TValue

字典中的值類型。

參數

dictionary
IReadOnlyDictionary<TKey,TValue>

具有 TKey 類型索引鍵和 TValue 類型值的字典。

key
TKey

要取得之值的索引鍵。

defaultValue
TValue

dictionary 找不到與所指定 key 建立關聯的值時,所要傳回的預設值。

傳回

TValue

TValue 執行個體。 當方法成功時,傳回之物件會是與所指定 key 建立關聯的值。 當方法失敗時,會傳回 defaultValue

例外狀況

dictionarynull

適用於

.NET 9 和其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1