using System.Collections.Generic; namespace Microsoft.Win32.TaskScheduler { /// /// Compares two arrays to see if the values inside of the array are the same. This is /// dependent on the type contained in the array having a valid Equals() override. /// internal static class ArrayComparer { public static IEnumerable Cast(this System.Collections.IEnumerable source) { if (source == null) throw new System.ArgumentNullException("source"); if (default(TResult) != null && source is IEnumerable) return source as IEnumerable; return CastImpl(source); } private static IEnumerable CastImpl(System.Collections.IEnumerable source) { foreach (object item in source) yield return (TResult)item; } public static IEnumerable OfType(this System.Collections.IEnumerable source) { if (source == null) throw new System.ArgumentNullException("source"); if (default(TResult) != null && source is IEnumerable) return source as IEnumerable; return OfTypeImpl(source); } private static IEnumerable OfTypeImpl(System.Collections.IEnumerable source) { foreach (object item in source) if (item is TResult) yield return (TResult)item; } /// /// Returns a hash code for this instance. /// /// /// The coll. /// /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. /// public static int GetItemHashCode(this IEnumerable coll) { // if non-null array then go into unchecked block to avoid overflow if (coll != null) { unchecked { int hash = 17; // get hash code for all items in array foreach (var item in coll) hash = hash * 23 + ((item != null) ? item.GetHashCode() : 0); return hash; } } // if null, hash code is zero return 0; } /// /// Compares the contents of both arrays to see if they are equal. This depends on having a valid override for Equals(). /// /// The first array to compare. /// The second array to compare. /// True if and have equal contents. public static bool Equals(this T[] firstArray, T[] secondArray) { return Equals(firstArray as IList, secondArray as IList); } /// /// Compares the contents of both lists to see if they are equal. This depends on having a valid override for Equals(). /// /// The first list to compare. /// The second list to compare. /// True if and have equal contents. public static bool Equals(this IList list1, IList list2) { // if same reference or both null, then equality is true if (object.ReferenceEquals(list1, list2)) return true; // otherwise, if both arrays have same length, compare all elements if (list1 != null && list2 != null && (list1.Count == list2.Count)) { for (int i = 0; i < list1.Count; i++) { // if any mismatch, not equal if (!object.Equals(list1[i], list2[i])) return false; } // if no mismatches, equal return true; } // if we get here, they are not equal return false; } } }