22 lines
739 B
C#
22 lines
739 B
C#
using System.Collections.Generic;
|
|
|
|
namespace LLama.Extensions
|
|
{
|
|
internal static class DictionaryExtensions
|
|
{
|
|
#if NETSTANDARD2_0
|
|
public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
|
|
{
|
|
return GetValueOrDefaultImpl(dictionary, key, defaultValue);
|
|
}
|
|
#elif !NET6_0_OR_GREATER && !NETSTANDARD2_1_OR_GREATER
|
|
#error Target framework not supported!
|
|
#endif
|
|
|
|
internal static TValue GetValueOrDefaultImpl<TKey, TValue>(IReadOnlyDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
|
|
{
|
|
return dictionary.TryGetValue(key, out var value) ? value : defaultValue;
|
|
}
|
|
}
|
|
}
|