CSCI 1301 – Lab 30

Clément Aubert

November 18, 2019

Comparing Two Arrays

Execute the following code:

int[] arrayA = { 1, 3, 4 };
int[] arrayB = { 1, 3, 4 };
if (arrayA == arrayB){Console.WriteLine("The arrays are the same!");}

and see for yourself that you can not use == to test for equality of arrays.

Then, like we did in class:

Once you are done with this, write two other copy methods taking three arguments: one for arrays of decimals, and one for arrays of strings. Can you give them the same name as for the one that takes arrays of integers as arguments? Why, or why not?

Pushing Further (Optional)

Have a look at https://docs.microsoft.com/en-us/dotnet/api/system.array.copy?view=netframework-4.8 and https://stackoverflow.com/q/17212254/ to see other implementations of methods to copy and compare arrays. Note that the solution discussed in the second post requires to use using System.Linq; at the beginning of your program:

using System;
using System.Linq;

class MainClass {
  public static void Main (string[] args) {

  int[] arrayA = { 1, 8, 9, 10, 1, 30, 1, 32, 3 };
  int[] arrayB = { 1, 8, 9, 10, 1, 30, 1, 32, 3 };
  int[] arrayC = { 1000000, 8, 9, 10, 1, 30, 1, 32, 3 };
  if (arrayA.SequenceEqual(arrayB)){
    Console.WriteLine("The arrays arrayA and arrayB are the same!");
    }
  if (arrayA.SequenceEqual(arrayC)){
    Console.WriteLine("The arrays arrayA and arrayC are the same!");
    }

  }
}