From 85fb7360b4b444c09d0cfead0a5b9b770e24afb3 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Tue, 14 Mar 2023 08:14:42 +0100 Subject: [PATCH] Add dictionary extension methods to get value or default --- .../Elsa.Workflows.Core/Extensions/DictionaryExtensions.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/modules/Elsa.Workflows.Core/Extensions/DictionaryExtensions.cs b/src/modules/Elsa.Workflows.Core/Extensions/DictionaryExtensions.cs index 316c8a0a4..59ae7ea44 100644 --- a/src/modules/Elsa.Workflows.Core/Extensions/DictionaryExtensions.cs +++ b/src/modules/Elsa.Workflows.Core/Extensions/DictionaryExtensions.cs @@ -22,6 +22,11 @@ public static class DictionaryExtensions public static T? GetValue(this IDictionary dictionary, TKey key) => ConvertValue(dictionary[key]); public static T? GetValue(this IDictionary dictionary, string key) => ConvertValue(dictionary[key]); + + public static T? GetValueOrDefault(this IDictionary dictionary, TKey key, Func defaultValueFactory) => TryGetValue(dictionary, key, out var value) ? value : defaultValueFactory(); + public static T? GetValueOrDefault(this IDictionary dictionary, TKey key) => GetValueOrDefault(dictionary, key, () => default); + public static T? GetValueOrDefault(this IDictionary dictionary, string key, Func defaultValueFactory) => TryGetValue(dictionary, key, out var value) ? value : defaultValueFactory(); + public static T? GetValueOrDefault(this IDictionary dictionary, string key) => GetValueOrDefault(dictionary, key, () => default); public static T GetOrAdd(this IDictionary dictionary, string key, Func valueFactory) where T : notnull => dictionary.GetOrAdd(key, valueFactory);