From f8d73f2fd7e49726582a831467cbbc95bda07f0a Mon Sep 17 00:00:00 2001 From: Blake Niemyjski Date: Mon, 18 Mar 2019 13:18:15 -0700 Subject: [PATCH] Improved equality checks --- .../CollectionEqualityExtensions.cs | 40 +++++++++++++++---- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/src/Exceptionless/Extensions/CollectionEqualityExtensions.cs b/src/Exceptionless/Extensions/CollectionEqualityExtensions.cs index 491365ec..1ec5d700 100644 --- a/src/Exceptionless/Extensions/CollectionEqualityExtensions.cs +++ b/src/Exceptionless/Extensions/CollectionEqualityExtensions.cs @@ -5,6 +5,12 @@ namespace Exceptionless { internal static class CollectionEqualityExtensions { public static bool CollectionEquals(this IEnumerable source, IEnumerable other) { + if (source == null && other == null) + return true; + + if (source == null || other == null) + return false; + var sourceEnumerator = source.GetEnumerator(); var otherEnumerator = other.GetEnumerator(); @@ -14,23 +20,29 @@ public static bool CollectionEquals(this IEnumerable source, IEnumerable(this IDictionary source, IDictionary other) { - if (source.Count != other.Count) { + if (source == null && other == null) + return true; + + if (source == null || other == null || source.Count != other.Count) return false; - } foreach (var key in source.Keys) { var sourceValue = source[key]; @@ -40,12 +52,26 @@ public static bool CollectionEquals(this IDictionary sou return false; } - if (sourceValue.Equals(otherValue)) { + if (sourceValue == null && otherValue == null) + continue; + + if (source == null || other == null || !sourceValue.Equals(otherValue)) return false; - } } + return true; } + + + public static bool CollectionEquals(this ISet source, ISet other) { + if (source == null && other == null) + return true; + + if (source == null || other == null || source.Count != other.Count) + return false; + + return source.SetEquals(other); + } public static int GetCollectionHashCode(this IEnumerable source) { var assemblyQualifiedName = typeof(T).AssemblyQualifiedName;