DictionaryExtensions.cs 775 B

12345678910111213141516171819202122232425
  1. using System.Collections.Generic;
  2. namespace NModbus.Extensions
  3. {
  4. internal static class DictionaryExtensions
  5. {
  6. /// <summary>
  7. /// Gets the specified value in the dictionary. If not found, returns default for TValue.
  8. /// </summary>
  9. /// <typeparam name="TKey"></typeparam>
  10. /// <typeparam name="TValue"></typeparam>
  11. /// <param name="dictionary"></param>
  12. /// <param name="key"></param>
  13. /// <returns></returns>
  14. internal static TValue? GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
  15. {
  16. TValue value;
  17. if (dictionary.TryGetValue(key, out value!))
  18. return value;
  19. return default(TValue);
  20. }
  21. }
  22. }