CollectionExtensions.GetValueOrDefault Method

Definition

Overloads

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

Tries to get the value associated with the specified key in the dictionary.

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

Tries to get the value associated with the specified key in the dictionary.

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

Source:
CollectionExtensions.cs
Source:
CollectionExtensions.cs
Source:
CollectionExtensions.cs

Tries to get the value associated with the specified key in the dictionary.

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);

Type Parameters

TKey

The type of the keys in the dictionary.

TValue

The type of the values in the dictionary.

Parameters

dictionary
IReadOnlyDictionary<TKey,TValue>

A dictionary with keys of type TKey and values of type TValue.

key
TKey

The key of the value to get.

Returns

TValue

A TValue instance. When the method is successful, the returned object is the value associated with the specified key. When the method fails, it returns the default value for TValue.

Exceptions

dictionary is null.

Applies to

.NET 9 and other versions
Product Versions
.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)

Source:
CollectionExtensions.cs
Source:
CollectionExtensions.cs
Source:
CollectionExtensions.cs

Tries to get the value associated with the specified key in the dictionary.

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

Type Parameters

TKey

The type of the keys in the dictionary.

TValue

The type of the values in the dictionary.

Parameters

dictionary
IReadOnlyDictionary<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

The default value to return when the dictionary cannot find a value associated with the specified key.

Returns

TValue

A TValue instance. When the method is successful, the returned object is the value associated with the specified key. When the method fails, it returns defaultValue.

Exceptions

dictionary is null.

Applies to

.NET 9 and other versions
Product Versions
.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