using System.Collections; using System.Collections.Generic; using System.Linq; namespace CommonLibrary { public static class CollectionTools { /// /// Checks whether a collection is the same as another collection /// /// The current instance object /// The collection to compare with /// The comparer object to use to compare each item in the collection. If null uses EqualityComparer.Default /// True if the two collections contain all the same items in the same order public static bool IsEqualTo(this IEnumerable value, IEnumerable compareList, IEqualityComparer comparer) { if (comparer == null) { comparer = EqualityComparer.Default; } if (compareList == null || value.Count() != compareList.Count()) { return false; } else { IEnumerator enumerator1 = value.GetEnumerator(); IEnumerator enumerator2 = compareList.GetEnumerator(); while (enumerator1.MoveNext() && enumerator2.MoveNext()) { if (!comparer.Equals(enumerator1.Current, enumerator2.Current)) { return false; } } return true; } } /// /// Checks whether a collection is the same as another collection /// /// The current instance object /// The collection to compare with /// True if the two collections contain all the same items in the same order public static bool IsEqualTo(this IEnumerable value, IEnumerable compareList) { return IsEqualTo(value, compareList, null); } /// /// Checks whether a collection is the same as another collection /// /// The current instance object /// The collection to compare with /// True if the two collections contain all the same items in the same order public static bool IsEqualTo(this IEnumerable value, IEnumerable compareList) { return IsEqualTo(value.OfType(), compareList.OfType()); } } }