Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ConcurrentDictionary GetOrAdd extension methods #8220

Merged
merged 1 commit into from
Mar 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static TValue GetOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, T
{
return dictionary.TryGetValue(key, out var obj) ? obj : default;
}

/// <summary>
/// Gets a value from the dictionary with given key. Returns default value if can not find.
/// </summary>
Expand Down Expand Up @@ -100,7 +100,7 @@ public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dicti

return dictionary[key] = factory(key);
}

/// <summary>
/// Gets a value from the dictionary with given key. Returns default value if can not find.
/// </summary>
Expand All @@ -114,5 +114,19 @@ public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dicti
{
return dictionary.GetOrAdd(key, k => factory());
}

/// <summary>
/// Gets a value from the concurrent dictionary with given key. Returns default value if can not find.
/// </summary>
/// <param name="dictionary">Concurrent dictionary to check and get</param>
/// <param name="key">Key to find the value</param>
/// <param name="factory">A factory method used to create the value if not found in the dictionary</param>
/// <typeparam name="TKey">Type of the key</typeparam>
/// <typeparam name="TValue">Type of the value</typeparam>
/// <returns>Value if found, default if can not found.</returns>
public static TValue GetOrAdd<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dictionary, TKey key, Func<TValue> factory)
{
return dictionary.GetOrAdd(key, k => factory());
}
}
}
}