Compare two arrays for equality in C#
This post will check if two arrays are equal or not in C#.
Two arrays are considered equal in C# if both sequences have equal length and contain identical data in the same order. We can check array equality using any of the following methods:
1. Using Enumerable.SequenceEqual() method
To check for array equality, we can use the Enumerable.SequenceEquals() method in the System.Linq namespace. This works by checking whether two source sequences are of equal length and their corresponding elements are equal according to the default equality comparer for their type.
The following example demonstrates the usage of the SequenceEqual() method to determine whether two sequences are equal.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; using System.Linq; public class Example { public static bool checkEquality<T>(T[] first, T[] second) { return Enumerable.SequenceEqual(first, second); } public static void Main() { int[] first = { 1, 2, 3, 4, 5 }; int[] second = { 1, 2, 3, 4, 5 }; if (checkEquality(first, second)) { Console.WriteLine("Both arrays are equal"); } else { Console.WriteLine("Both arrays are not equal"); } } } |
The above code can be rewritten as follow:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; using System.Linq; public class Example { public static bool checkEquality<T>(T[] first, T[] second) { return first.SequenceEqual(second); } public static void Main() { int[] first = { 1, 2, 3, 4, 5 }; int[] second = { 1, 2, 3, 4, 5 }; if (checkEquality(first, second)) { Console.WriteLine("Both arrays are equal"); } else { Console.WriteLine("Both arrays are not equal"); } } } |
2. Custom Routine
We can also write our own custom routine to compare two generic arrays. The routine should determine whether the compared sequences contain references to the same objects and compare the objects’ actual data within the sequences. To compare the objects’ actual data in the sequences, we can use EqualityComparer<T>.
This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
using System; using System.Collections.Generic; public class Example { public static bool checkEquality<T>(T[] first, T[] second) { if (ReferenceEquals(first, second)) { return true; } if (first == null || second == null || (first.Length != second.Length)) { return false; } EqualityComparer<T> comparer = EqualityComparer<T>.Default; for (int i = 0; i < first.Length; i++) { if (!comparer.Equals(first[i], second[i])) { return false; } } return true; } public static void Main() { int[] first = { 1, 2, 3, 4, 5 }; int[] second = { 1, 2, 3, 4, 5 }; if (checkEquality(first, second)) { Console.WriteLine("Both arrays are equal"); } else { Console.WriteLine("Both arrays are not equal"); } } } |
That’s all about comparing two arrays for equality in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)