mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-08-31 19:28:18 +00:00
feat: Added dictionary extension for removing a key and outputting the value
This commit is contained in:
parent
3da9b23e12
commit
6f5deaf1ea
1 changed files with 30 additions and 2 deletions
|
|
@ -7,6 +7,29 @@ namespace ICD.Common.Utils.Extensions
|
|||
{
|
||||
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>
|
||||
/// Removes all of the given keys from the dictionary.
|
||||
/// </summary>
|
||||
|
|
@ -78,7 +101,9 @@ namespace ICD.Common.Utils.Extensions
|
|||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
if (key == null)
|
||||
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||
throw new ArgumentNullException("key");
|
||||
|
||||
return extends.GetDefault(key, default(TValue));
|
||||
|
|
@ -101,7 +126,9 @@ namespace ICD.Common.Utils.Extensions
|
|||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
if (key == null)
|
||||
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||
throw new ArgumentNullException("key");
|
||||
|
||||
TValue value;
|
||||
|
|
@ -119,13 +146,14 @@ namespace ICD.Common.Utils.Extensions
|
|||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static TValue GetOrAddDefault<TKey, TValue>([NotNull] this IDictionary<TKey, TValue> extends,
|
||||
[NotNull] TKey key,
|
||||
TValue defaultValue)
|
||||
[NotNull] TKey key, TValue defaultValue)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
if (key == null)
|
||||
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||
throw new ArgumentNullException("key");
|
||||
|
||||
TValue value = extends.GetDefault(key, defaultValue);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue