ExtensionMethods.cs 632 B

12345678910111213141516171819202122
  1. using System;
  2. using System.Collections.Generic;
  3. namespace DhcpServer.Utils
  4. {
  5. internal static class ExtensionMethods
  6. {
  7. public static TR MapValue<TK, TV, TR>(this Dictionary<TK, TV> dic, TK key, Func<TV, TR> found)
  8. {
  9. return dic.TryGetValue(key, out var value)
  10. ? found(value)
  11. : default;
  12. }
  13. public static TR MapValue<TK, TV, TR>(this Dictionary<TK, TV> dic, TK key, Func<TV, TR> found, Func<TR> notFound)
  14. {
  15. return dic.TryGetValue(key, out var value)
  16. ? found(value)
  17. : notFound();
  18. }
  19. }
  20. }