Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 in C#, you need to understand the difference between reference equality and value equality. The Equals() method checks reference equality by default, meaning it returns true only if both variables point to the same array object in memory.
For comparing array contents (value equality), you need to use methods like SequenceEqual() from LINQ or implement custom comparison logic.
Reference Equality vs Value Equality
Using Equals() Method (Reference Equality)
Different Arrays with Same Contents
using System;
public class Demo {
public static void Main() {
String[] strArr1 = new String[3] { "John", "Jacob", "Tim"};
String[] strArr2 = new String[3] { "John", "Jacob", "Tim"};
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));
}
}
The output of the above code is −
First String array... John Jacob Tim Second String array... John Jacob Tim Are both the array objects equal? = False
Same Array Reference
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; // Now both point to same array
Console.WriteLine("Are both the array objects equal? = " + arr1.Equals(arr2));
}
}
The output of the above code is −
First integer array... 10 20 30 40 50 Second integer array... 25 25 40 55 70 Are both the array objects equal? = True
Using SequenceEqual() for Value Equality
using System;
using System.Linq;
public class Demo {
public static void Main() {
int[] arr1 = new int[] { 10, 20, 30 };
int[] arr2 = new int[] { 10, 20, 30 };
int[] arr3 = new int[] { 10, 30, 20 };
Console.WriteLine("Array 1: [" + string.Join(", ", arr1) + "]");
Console.WriteLine("Array 2: [" + string.Join(", ", arr2) + "]");
Console.WriteLine("Array 3: [" + string.Join(", ", arr3) + "]");
Console.WriteLine("arr1.Equals(arr2): " + arr1.Equals(arr2));
Console.WriteLine("arr1.SequenceEqual(arr2): " + arr1.SequenceEqual(arr2));
Console.WriteLine("arr1.SequenceEqual(arr3): " + arr1.SequenceEqual(arr3));
}
}
The output of the above code is −
Array 1: [10, 20, 30] Array 2: [10, 20, 30] Array 3: [10, 30, 20] arr1.Equals(arr2): False arr1.SequenceEqual(arr2): True arr1.SequenceEqual(arr3): False
Comparison of Array Equality Methods
| Method | Type of Equality | When Returns True |
|---|---|---|
Equals() |
Reference Equality | Only when both variables point to the same array object |
SequenceEqual() |
Value Equality | When arrays have same elements in same order |
ReferenceEquals() |
Reference Equality | Same as Equals() for arrays |
Conclusion
Array equality in C# depends on whether you want reference equality (same object) or value equality (same contents). Use Equals() for reference comparison and SequenceEqual() from LINQ for content comparison. Understanding this difference is crucial for proper array comparisons.
