Dictionary.Update extension method returns a bool for change state

This commit is contained in:
Chris Cameron
2018-02-22 16:56:10 -05:00
parent b0aa48803b
commit 282d8e4d0e
2 changed files with 36 additions and 2 deletions

View File

@@ -168,7 +168,8 @@ namespace ICD.Common.Utils.Tests.Extensions
{3, 30}
};
a.Update(b);
Assert.IsTrue(a.Update(b));
Assert.IsFalse(a.Update(b));
Assert.AreEqual(3, a.Count);
Assert.AreEqual(10, a[1]);

View File

@@ -191,8 +191,9 @@ namespace ICD.Common.Utils.Extensions
/// <typeparam name="TValue"></typeparam>
/// <param name="extends"></param>
/// <param name="other"></param>
/// <returns></returns>
[PublicAPI]
public static void Update<TKey, TValue>(this IDictionary<TKey, TValue> extends, IDictionary<TKey, TValue> other)
public static bool Update<TKey, TValue>(this IDictionary<TKey, TValue> extends, IDictionary<TKey, TValue> other)
{
if (extends == null)
throw new ArgumentNullException("extends");
@@ -200,8 +201,40 @@ namespace ICD.Common.Utils.Extensions
if (other == null)
throw new ArgumentNullException("other");
return extends.Update(other, EqualityComparer<TValue>.Default);
}
/// <summary>
/// Updates the dictionary with items from the other dictionary.
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
/// <param name="extends"></param>
/// <param name="other"></param>
/// <param name="comparer"></param>
/// <returns></returns>
[PublicAPI]
public static bool Update<TKey, TValue>(this IDictionary<TKey, TValue> extends, IDictionary<TKey, TValue> other,
IEqualityComparer<TValue> comparer)
{
if (extends == null)
throw new ArgumentNullException("extends");
if (other == null)
throw new ArgumentNullException("other");
bool change = false;
foreach (KeyValuePair<TKey, TValue> pair in other)
{
if (extends.ContainsKey(pair.Key) && comparer.Equals(pair.Value, extends[pair.Key]))
continue;
extends[pair.Key] = pair.Value;
change = true;
}
return change;
}
/// <summary>