Skip to content

Commit

Permalink
DictionaryExtensions.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
Nixill committed Dec 12, 2024
1 parent bfefd06 commit 8119dd4
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions CSharp.Nixill/src/Utils/Extensions/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Nixill.Utils.Extensions;

public static class DictionaryExtensions
{
public static V GetOrSet<K, V>(this IDictionary<K, V> dictionary, K key, V value)
=> dictionary.GetOrSet(key, () => value);

public static V GetOrSet<K, V>(this IDictionary<K, V> dictionary, K key, Func<V> value)
{
V returnValue;

if (!dictionary.TryGetValue(key, out returnValue!))
{
returnValue = value();
dictionary[key] = returnValue;
}

return returnValue;
}
}

0 comments on commit 8119dd4

Please sign in to comment.