Compares part of two arrays for equality. - CSharp System

CSharp examples for System:Array Equal

Description

Compares part of two arrays for equality.

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using System;//from  ww w .  ja v a2 s.  c  om

public class Main{
        /// <summary>
        /// Compares part of two arrays for equality.
        /// </summary>
        public static bool AreEqual(byte[] array1, int offset1, byte[] array2, int offset2, int length)
        {
            // Check length does not exceed boundaries
            if (offset1 + length > array1.Length || offset2 + length > array2.Length)
                return false;

            // Compare array contents
            for (var i = 0; i < length; i++)
            {
                if (array1[offset1 + i] != array2[offset2 + i])
                    return false;
            }
            return true;
        }
        /// <summary>
        /// Compares two collections by value.
        /// </summary>
        public static bool AreEqual(IEnumerable enumeration1, IEnumerable enumeration2)
        {
            // Compare null
            if (enumeration1 == null)
                return enumeration2 == null;
            if (enumeration2 == null)
                return false;

            // Compare values
            var enumerator1 = enumeration1.GetEnumerator();
            var enumerator2 = enumeration2.GetEnumerator();
            do
            {
                // Get next item and check length
                var more1 = enumerator1.MoveNext();
                var more2 = enumerator2.MoveNext();
                if (more1 != more2)
                {
                    // Different lengths
                    return false;
                }
                if (!more1)
                {
                    // End with no differences
                    return true;
                }

                // Compare current values
                var value1 = enumerator1.Current;
                var value2 = enumerator2.Current;
                if (!ReferenceEquals(value1, null))
                {
                    // Compare nested array by value too
                    if (value1.GetType().IsArray)
                        return AreEqual((Array)value1, (Array)value2);

                    // Compare other objects using any defined comparer or operator overloads
                    // This will still compare reference types by reference when none are defined
                    if (!value1.Equals(value2))
                        return false;
                }
                else if (!ReferenceEquals(value2, null))
                    return false;

                // Next...
            } while (true);
        }
        #region Public Methods

        /// <summary>
        /// Compares two list based arrays by value.
        /// </summary>
        public static bool AreEqual(IList array1, IList array2)
        {
            // Compare null
            if (array1 == null)
                return array2 == null;
            if (array2 == null)
                return false;

            // Compare length
            if (array1.Count != array2.Count)
                return false;

            // Compare values
            for (var index = 0; index < array1.Count; index++)
            {
                var value1 = array1[index];
                var value2 = array2[index];
                if (!ReferenceEquals(value1, null))
                {
                    // Compare nested array by value too
                    if (value1.GetType().IsArray)
                        return AreEqual((Array)value1, (Array)value2);

                    // Compare other objects using any defined comparer or operator overloads
                    // This will still compare reference types by reference when none are defined
                    if (!value1.Equals(value2))
                        return false;
                }
                else if (!ReferenceEquals(value2, null))
                    return false;
            }

            // Return same
            return true;
        }
}

Related Tutorials