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:
// 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)); } } } |
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 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)); } } } |
False False True
Note: If the current instance is a reference type, the Equals(Object) method checks for reference equality.
Recommended Posts:
- Array.BinarySearch(Array, Object) Method with examples in C#
- C# | Array.BinarySearch(Array, Object, IComparer) Method
- Object and Dynamic Array in C#
- C# | Array.BinarySearch(Array, Int32, Int32, Object, IComparer) Method
- Array.BinarySearch(Array, Int32, Int32, Object) Method with examples in C#
- C# | Copying the SortedList elements to an Array Object
- C# | Check if a SortedList object is synchronized
- C# | Check whether a SortedList object contains a specific key
- C# | Check if a SortedList object contains a specific value
- C# | Check if a SortedList object has a fixed size
- C# | Check if a SortedSet object is a proper subset of the specified collection
- C# | Check if a SortedSet object is a proper superset of the specified collection
- C# | Add an object to the end of Collection<T>
- C# | Add an object to the end of the ArrayList
- C# | Class and Object
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.