A method that compares each object in two arrays to see if the arrays are equal - CSharp System

CSharp examples for System:Array Equal

Description

A method that compares each object in two arrays to see if the arrays are equal

Demo Code


using System.Collections.Generic;

public class Main{
        /// <summary>
        ///     A method that compares each object in two arrays to see if the arrays are equal
        /// </summary>
        /// <typeparam name="T">the class type of the array</typeparam>
        /// <param name="array1">the first array to be compared</param>
        /// <param name="array2">the second array to be compared</param>
        /// <returns></returns>
        public static bool AreEqual<T>(T[] array1, T[] array2)
        {/*w  w w  .j a  v a 2  s  . com*/
            //Check they are the same length
            if (array1.Length != array2.Length)
            {
                return false;
            }
            //Check all elements are the same
            for (int i = 0; i < array1.Length; i++)
            {
                if (!array1[i].Equals(array2[i]))
                {
                    return false;
                }
            }
            return true;
        }
}

Related Tutorials