C# | Check if an array object is equal to another array object
Last Updated : 06 Jan, 2022
Improve
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:
// 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));
}
}
}
Output:
True
Example 2:
// 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 initializing 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));
}
}
}
Output:
False False True
Note: If the current instance is a reference type, the Equals(Object) method checks for reference equality.