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

An array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. Equals(Object) method which is inherited by Array class from object class is used to check whether an array is equal to another array or not.

Syntax:

public virtual bool Equals (object obj);

Here, obj is the object which is to be compared with the current object.



Return Value: This method return true if the specified object is equal to the current object otherwise it returns false.

Below programs illustrate the use of above-discussed method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to check if a Array is
// equal to other Array or not
using System;
  
namespace geeksforgeeks {
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // Two-dimensional array
        int[, ] intarray = new int[, ] { { 1, 2 },
                                         { 3, 4 },
                                         { 5, 6 },
                                         { 7, 8 } };
  
        // check if intarray is
        // equal to itself or not
        Console.WriteLine(intarray.Equals(intarray));
    }
}
}

chevron_right


Output:

True

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to check if an Array is
// equal to other Array or not
using System;
  
namespace geeksforgeeks {
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
      
        // Creating and intializing new the String
        String[] arr1 = new String[4] { "Sun", "Mon", "Tue", "Thu" };
          
          
        // taking anotther array
        String[] arr2 = new String[4] { "Sun", "Mon", "Tue", "Thu" };
          
  
        // check if arr1 is
        // equal to arr2 or not
        Console.WriteLine(arr1.Equals(arr2));
          
        // assigning arr1 reference to arr3
        String[] arr3 = new String[4];
        arr3 = arr1;
          
        // check if arr2 is
        // equal to arr3 or not
        Console.WriteLine(arr2.Equals(arr3));
          
        // check if arr1 is
        // equal to arr3 or not
        // it will return true as both 
        // array object refer to same
        Console.WriteLine(arr1.Equals(arr3));
          
    }
}
}

chevron_right


Output:

False
False
True

Note: If the current instance is a reference type, the Equals(Object) method checks for reference equality.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.