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
How to compare two arrays in C#?
Comparing arrays in C# can be done using several approaches. The most common and efficient method is using the SequenceEqual() method from LINQ, but you can also compare arrays manually using loops or other techniques.
Syntax
Following is the syntax for using SequenceEqual() to compare two arrays −
bool result = array1.SequenceEqual(array2);
Following is the syntax for manual comparison using a loop −
bool areEqual = true;
for (int i = 0; i < array1.Length; i++) {
if (array1[i] != array2[i]) {
areEqual = false;
break;
}
}
Using SequenceEqual() Method
The SequenceEqual() method from LINQ compares two sequences element by element and returns true if they are equal in length and content −
using System;
using System.Linq;
class Program {
static void Main(string[] args) {
// two arrays
int[] arr = new int[] { 99, 87, 56, 45 };
int[] brr = new int[] { 99, 87, 56, 45 };
int[] crr = new int[] { 99, 87, 56, 40 };
// compare identical arrays
Console.WriteLine("arr == brr: " + arr.SequenceEqual(brr));
// compare different arrays
Console.WriteLine("arr == crr: " + arr.SequenceEqual(crr));
}
}
The output of the above code is −
arr == brr: True arr == crr: False
Using Manual Loop Comparison
You can also compare arrays manually using a loop, which gives you more control over the comparison logic −
using System;
class Program {
static void Main(string[] args) {
int[] arr1 = { 10, 20, 30, 40 };
int[] arr2 = { 10, 20, 30, 40 };
int[] arr3 = { 10, 20, 30, 50 };
Console.WriteLine("arr1 == arr2: " + CompareArrays(arr1, arr2));
Console.WriteLine("arr1 == arr3: " + CompareArrays(arr1, arr3));
}
static bool CompareArrays(int[] array1, int[] array2) {
if (array1.Length != array2.Length) {
return false;
}
for (int i = 0; i
The output of the above code is −
arr1 == arr2: True
arr1 == arr3: False
Using Array.Equals() with Different Data Types
Here's an example comparing string arrays using SequenceEqual() −
using System;
using System.Linq;
class Program {
static void Main(string[] args) {
string[] names1 = { "Alice", "Bob", "Charlie" };
string[] names2 = { "Alice", "Bob", "Charlie" };
string[] names3 = { "Alice", "Bob", "David" };
Console.WriteLine("names1 == names2: " + names1.SequenceEqual(names2));
Console.WriteLine("names1 == names3: " + names1.SequenceEqual(names3));
// Case-sensitive comparison
string[] names4 = { "alice", "bob", "charlie" };
Console.WriteLine("names1 == names4: " + names1.SequenceEqual(names4));
}
}
The output of the above code is −
names1 == names2: True
names1 == names3: False
names1 == names4: False
Comparison of Methods
| Method | Advantages | Disadvantages |
|---|---|---|
| SequenceEqual() | Simple, concise, handles null arrays gracefully | Requires System.Linq namespace |
| Manual Loop | No external dependencies, customizable logic | More code, need to handle edge cases manually |
Conclusion
The SequenceEqual() method is the most convenient way to compare arrays in C# as it handles length checking and element comparison automatically. For simple equality checks, use SequenceEqual(), but implement manual comparison when you need custom logic or want to avoid LINQ dependencies.
