mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 13:15:07 +00:00
feat: Added dictionary extension for removing a key and outputting the value
This commit is contained in:
@@ -7,6 +7,29 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
{
|
{
|
||||||
public static class DictionaryExtensions
|
public static class DictionaryExtensions
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Removes the key from the dictionary, outputting the value.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TKey"></typeparam>
|
||||||
|
/// <typeparam name="TValue"></typeparam>
|
||||||
|
/// <param name="extends"></param>
|
||||||
|
/// <param name="key"></param>
|
||||||
|
/// <param name="value"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool Remove<TKey, TValue>([NotNull] this IDictionary<TKey, TValue> extends,
|
||||||
|
[NotNull] TKey key, out TValue value)
|
||||||
|
{
|
||||||
|
if (extends == null)
|
||||||
|
throw new ArgumentNullException("extends");
|
||||||
|
|
||||||
|
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||||
|
if (key == null)
|
||||||
|
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||||
|
throw new ArgumentNullException("key");
|
||||||
|
|
||||||
|
return extends.TryGetValue(key, out value) && extends.Remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Removes all of the given keys from the dictionary.
|
/// Removes all of the given keys from the dictionary.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -78,7 +101,9 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
if (extends == null)
|
if (extends == null)
|
||||||
throw new ArgumentNullException("extends");
|
throw new ArgumentNullException("extends");
|
||||||
|
|
||||||
|
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||||
if (key == null)
|
if (key == null)
|
||||||
|
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||||
throw new ArgumentNullException("key");
|
throw new ArgumentNullException("key");
|
||||||
|
|
||||||
return extends.GetDefault(key, default(TValue));
|
return extends.GetDefault(key, default(TValue));
|
||||||
@@ -101,7 +126,9 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
if (extends == null)
|
if (extends == null)
|
||||||
throw new ArgumentNullException("extends");
|
throw new ArgumentNullException("extends");
|
||||||
|
|
||||||
|
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||||
if (key == null)
|
if (key == null)
|
||||||
|
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||||
throw new ArgumentNullException("key");
|
throw new ArgumentNullException("key");
|
||||||
|
|
||||||
TValue value;
|
TValue value;
|
||||||
@@ -119,13 +146,14 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public static TValue GetOrAddDefault<TKey, TValue>([NotNull] this IDictionary<TKey, TValue> extends,
|
public static TValue GetOrAddDefault<TKey, TValue>([NotNull] this IDictionary<TKey, TValue> extends,
|
||||||
[NotNull] TKey key,
|
[NotNull] TKey key, TValue defaultValue)
|
||||||
TValue defaultValue)
|
|
||||||
{
|
{
|
||||||
if (extends == null)
|
if (extends == null)
|
||||||
throw new ArgumentNullException("extends");
|
throw new ArgumentNullException("extends");
|
||||||
|
|
||||||
|
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||||
if (key == null)
|
if (key == null)
|
||||||
|
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||||
throw new ArgumentNullException("key");
|
throw new ArgumentNullException("key");
|
||||||
|
|
||||||
TValue value = extends.GetDefault(key, defaultValue);
|
TValue value = extends.GetDefault(key, defaultValue);
|
||||||
|
|||||||
Reference in New Issue
Block a user