Check if an array object is equal to another array object in C#


To check if an array object is equal to another array object, the code is as follows −

Example

 Live Demo

using System;
public class Demo {
   public static void Main(){
      String[] strArr1 = new String[3] { "John", "Jacob", "Tim"};
      String[] strArr2 = new String[3] { "Tom","Brad","Bradley"};
      Console.WriteLine("First String array...");
      foreach(string val in strArr1){
         Console.WriteLine(val);
      }
      Console.WriteLine("Second String array...");
      foreach(string val in strArr2){
         Console.WriteLine(val);
      }
      Console.WriteLine("Are both the array objects equal? = "+strArr1.Equals(strArr2));
   }
}

Output

This will produce the following output −

First String array...
John
Jacob
Tim
Second String array...
Tom
Brad
Bradley
Are both the array objects equal? = False

Example

Let us see another example −

 Live Demo

using System;
public class Demo {
   public static void Main(){
      int[] arr1 = new int[5] { 10, 20, 30, 40, 50};
      int[] arr2 = new int[5] { 25, 25, 40, 55, 70};
      Console.WriteLine("First integer array...");
      foreach(int val in arr1){
         Console.WriteLine(val);
      }
      Console.WriteLine("Second integer array...");
      foreach(int val in arr2){
         Console.WriteLine(val);
      }
      arr1 = arr2;
      Console.WriteLine("Are both the array objects equal? = "+arr1.Equals(arr2));
   }
}

Output

This will produce the following output −

First integer array...
10
20
30
40
50
Second integer array...
25
25
40
55
70
Are both the array objects equal? = True
raja
Published on 04-Dec-2019 06:32:51
Advertisements