From ac5da50e98185064fc6b65b7da8fe19fdce795a1 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Mon, 16 Mar 2020 10:07:15 -0400 Subject: [PATCH] feat: Added IsDistinct extension method --- .../Extensions/EnumerableExtensions.cs | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs index df1cac4..2fc8418 100644 --- a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs +++ b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs @@ -966,6 +966,52 @@ namespace ICD.Common.Utils.Extensions return output; } + /// + /// Returns true if all of the items in the sequence are equal, or the sequence is empty. + /// + /// + /// + /// + public static bool IsDistinct([NotNull] this IEnumerable extends) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + IEqualityComparer comparer = EqualityComparer.Default; + return extends.IsDistinct(comparer); + } + + /// + /// Returns true if all of the items in the sequence are equal, or the sequence is empty. + /// + /// + /// + /// + /// + public static bool IsDistinct([NotNull] this IEnumerable extends, [NotNull] IEqualityComparer comparer) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + TItem other = default(TItem); + bool first = true; + + foreach (TItem item in extends) + { + if (first) + { + other = item; + first = false; + continue; + } + + if (!comparer.Equals(item, other)) + return false; + } + + return true; + } + /// /// Gets distinct elements from the sequence based on given property. ///