Compare two array - CSharp Language Basics

CSharp examples for Language Basics:Array

Description

Compare two array

Demo Code

using System;/* ww  w .  j  a v a2s  .co m*/
class ValueEquality
{
   public static void Main()
   {
      int [] myArray = {10, 20, 30, 40, 50, 60};
      int [] yourArray = {10, 20, 30, 40, 50, 60};
      if (ValueEquals (myArray, yourArray))
         Console.WriteLine("myArray is value equal to yourArray");
      else
         Console.WriteLine("myArray is not value equal to yourArray");
      if (myArray == yourArray)
            Console.WriteLine("myArray is reference equal to yourArray");
      else
            Console.WriteLine("myArray is not reference equal to yourArray");
    }
    private static bool ValueEquals (int [] array1, int [] array2)
    {
            bool areValueEqual = true;
            if (!(array1.Length == array2.Length))
            {
               areValueEqual = false;
            }
            else
            {
               for (int i = 0; i < array1.Length; i++)
               {
                  if (!(array1[i] == array2[i]))
                  {
                     areValueEqual = false;
                     break;
                  }
               }
            }
            return areValueEqual;
    }
}

Result


Related Tutorials