public bool Contains(T item) { var buffer = this.m_value; var length = buffer.Length; // null check var count = length; if (null != item) { System.Collections.Generic.EqualityComparer <T> c = System.Collections.Generic.EqualityComparer <T> .Default; for (var i = 0; count > i; ++i) { if (c.Equals(buffer[i], item)) { return(true); } } return(false); } { for (var i = 0; count > i; ++i) { if (null == buffer[i]) { return(true); } } return(false); } }
public static bool bIsArraysEqual <T>(T[] oArr1, T[] oArr2) { bool result; if (object.ReferenceEquals(oArr1, oArr2)) { result = true; } else if (oArr1 == null || oArr2 == null) { result = false; } else if (oArr1.Length != oArr2.Length) { result = false; } else { System.Collections.Generic.EqualityComparer <T> comparer = System.Collections.Generic.EqualityComparer <T> .Default; for (int nArryItem = 0; nArryItem < oArr1.Length; nArryItem++) { if (!comparer.Equals(oArr1[nArryItem], oArr2[nArryItem])) { result = false; return(result); } } result = true; } return(result); }
/// <summary>Compares the contents of a <see cref="System.Collections.Generic.IEnumerable{T}"/> /// implementation to another one to determine equality.</summary> /// <remarks>Thinking of the <see cref="System.Collections.Generic.IEnumerable{T}"/> implementation as /// a string with any number of characters, the algorithm checks /// each item in each list. If any item of the list is not equal (or /// one list contains all the elements of another list), then that list /// element is compared to the other list element to see which /// list is greater.</remarks> /// <param name="x">The <see cref="System.Collections.Generic.IEnumerable{T}"/> implementation /// that is considered the left hand side.</param> /// <param name="y">The <see cref="System.Collections.Generic.IEnumerable{T}"/> implementation /// that is considered the right hand side.</param> /// <returns>True if the items are equal, false otherwise.</returns> private static bool Equals(System.Collections.Generic.IEnumerable <T> x, System.Collections.Generic.IEnumerable <T> y) { // If x and y are null, then return true, they are the same. if (x == null && y == null) { // They are the same, return 0. return(true); } // If one is null, then return a value based on whether or not // one is null or not. if (x == null || y == null) { // Return false, one is null, the other is not. return(false); } // Check to see if the counts on the IEnumerable implementations are equal. // This is a shortcut, if they are not equal, then the lists are not equal. // If the result is indeterminate, then get out. bool?enumerableCountsEqual = EnumerableCountsEqual(x, y); // If the enumerable counts have been able to be calculated (indicated by // a non-null value) and it is false, then no need to iterate through the items. if (enumerableCountsEqual != null && !enumerableCountsEqual.Value) { // The sequences are not equal. return(false); } // The counts of the items in the enumerations are equal, or indeterminate // so a full iteration needs to be made to compare each item. // Get the default comparer for T first. System.Collections.Generic.EqualityComparer <T> defaultComparer = EqualityComparer <T> .Default; // Get the enumerator for y. System.Collections.Generic.IEnumerator <T> otherEnumerator = y.GetEnumerator(); // Call Dispose on IDisposable if there is an implementation on the // IEnumerator<T> returned by a call to y.GetEnumerator(). using (otherEnumerator as IDisposable) { // Cycle through the items in this list. foreach (T item in x) { // If there isn't an item to get, then this has more // items than that, they are not equal. if (!otherEnumerator.MoveNext()) { // Return false. return(false); } // Perform a comparison. Must check this on the left hand side // and that on the right hand side. bool comparison = defaultComparer.Equals(item, otherEnumerator.Current); // If the value is false, return false. if (!comparison) { // Return the value. return(comparison); } } // If there are no more items, then return true, the sequences // are equal. if (!otherEnumerator.MoveNext()) { // The sequences are equal. return(true); } // The other sequence has more items than this one, return // false, these are not equal. return(false); } }
public EqualityComparer(System.Collections.Generic.EqualityComparer <T> comparer) { this.comparer = comparer; }