You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

DictionaryExtensions.cs 739 B

123456789101112131415161718192021
  1. using System.Collections.Generic;
  2. namespace LLama.Extensions
  3. {
  4. internal static class DictionaryExtensions
  5. {
  6. #if NETSTANDARD2_0
  7. public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
  8. {
  9. return GetValueOrDefaultImpl(dictionary, key, defaultValue);
  10. }
  11. #elif !NET6_0_OR_GREATER && !NETSTANDARD2_1_OR_GREATER
  12. #error Target framework not supported!
  13. #endif
  14. internal static TValue GetValueOrDefaultImpl<TKey, TValue>(IReadOnlyDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
  15. {
  16. return dictionary.TryGetValue(key, out var value) ? value : defaultValue;
  17. }
  18. }
  19. }