Skip to content

Commit f8d73f2

Browse files
committed
Improved equality checks
1 parent ab9987e commit f8d73f2

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

src/Exceptionless/Extensions/CollectionEqualityExtensions.cs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
namespace Exceptionless {
66
internal static class CollectionEqualityExtensions {
77
public static bool CollectionEquals<T>(this IEnumerable<T> source, IEnumerable<T> other) {
8+
if (source == null && other == null)
9+
return true;
10+
11+
if (source == null || other == null)
12+
return false;
13+
814
var sourceEnumerator = source.GetEnumerator();
915
var otherEnumerator = other.GetEnumerator();
1016

@@ -14,23 +20,29 @@ public static bool CollectionEquals<T>(this IEnumerable<T> source, IEnumerable<T
1420
return false;
1521
}
1622

17-
if (sourceEnumerator.Current.Equals(otherEnumerator.Current)) {
18-
// values aren't equal
23+
var sourceValue = sourceEnumerator.Current;
24+
var otherValue = otherEnumerator.Current;
25+
if (sourceValue == null && otherValue == null)
26+
continue;
27+
28+
if (source == null || other == null || !sourceValue.Equals(otherValue))
1929
return false;
20-
}
2130
}
2231

2332
if (otherEnumerator.MoveNext()) {
2433
// counts differ
2534
return false;
2635
}
36+
2737
return true;
2838
}
2939

3040
public static bool CollectionEquals<TValue>(this IDictionary<string, TValue> source, IDictionary<string, TValue> other) {
31-
if (source.Count != other.Count) {
41+
if (source == null && other == null)
42+
return true;
43+
44+
if (source == null || other == null || source.Count != other.Count)
3245
return false;
33-
}
3446

3547
foreach (var key in source.Keys) {
3648
var sourceValue = source[key];
@@ -40,12 +52,26 @@ public static bool CollectionEquals<TValue>(this IDictionary<string, TValue> sou
4052
return false;
4153
}
4254

43-
if (sourceValue.Equals(otherValue)) {
55+
if (sourceValue == null && otherValue == null)
56+
continue;
57+
58+
if (source == null || other == null || !sourceValue.Equals(otherValue))
4459
return false;
45-
}
4660
}
61+
4762
return true;
4863
}
64+
65+
66+
public static bool CollectionEquals<TValue>(this ISet<TValue> source, ISet<TValue> other) {
67+
if (source == null && other == null)
68+
return true;
69+
70+
if (source == null || other == null || source.Count != other.Count)
71+
return false;
72+
73+
return source.SetEquals(other);
74+
}
4975

5076
public static int GetCollectionHashCode<T>(this IEnumerable<T> source) {
5177
var assemblyQualifiedName = typeof(T).AssemblyQualifiedName;

0 commit comments

Comments
 (0)