Compare arrays in C#
This post will discuss how to compare arrays in C#. The solution checks for structural equality, where two arrays are considered equal if they have equal values.
The == operator checks for reference equality in C#. To check for structural equality, you can use any of the following methods.
1. Using Enumerable.SequenceEqual Method
The Enumerable.SequenceEqual method in LINQ can check whether two arrays are equal. This method is available with .NET framework 3.5 or higher. It works by comparing corresponding elements of both sequences using the custom or default equality comparer.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; using System.Linq; public class Example { public static void Main() { int[] first = { 1, 4, 5, 2, 4 }; int[] second = { 1, 4, 5, 2, 4 }; bool isEqual = Enumerable.SequenceEqual(first, second); Console.WriteLine(isEqual); // True } } |
If the array is not null, you can directly call the SequenceEqual() method upon an array to compare its contents with the specified array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; using System.Linq; public class Example { public static void Main() { int[] first = { 1, 4, 5, 2, 4 }; int[] second = { 1, 4, 5, 2, 4 }; bool isEqual = first.SequenceEqual(second); Console.WriteLine(isEqual); // True } } |
To determine if two arrays have the same elements, compare their sorted copies with the SequenceEqual() method like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; using System.Linq; public class Example { public static void Main() { int[] first = { 1, 4, 5, 2, 4 }; int[] second = { 1, 4, 5, 2, 4 }; bool isEqual = first.OrderBy(a => a).SequenceEqual(second.OrderBy(a => a)); Console.WriteLine(isEqual); // True } } |
2. Using string.Join Method
Alternatively, you can just compare the string representation of both arrays with the == operator. The following solution uses String.Join method to concatenate elements of the specified array with the specified separator.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; public class Example { public static void Main() { int[] first = { 1, 4, 5, 2, 4 }; int[] second = { 1, 4, 5, 2, 4 }; bool isEqual = string.Join(",", first) == string.Join(",", second); Console.WriteLine(isEqual); // True } } |
3. Using custom routine
Finally, you can write a custom routine for checking the equality of two arrays. The idea is to iterate over the array elements and invoke the Equals() method on each element. This can be implemented as follows in C#.
|
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 40 41 42 |
using System; using System.Collections.Generic; public static class Extensions { public static bool isEqual<T>(this T[] first, T[] second) { if (first == null && second == null) { return true; } if (first == null || second == null) { return false; } if (first.Length != second.Length) { return false; } var comparer = EqualityComparer<T>.Default; for (int i = 0; i < first.Length; i++) { if (!comparer.Equals(first[i], second[i])) { return false; } } return true; } } public class Example { public static void Main() { int[] first = { 1, 4, 5, 2, 4 }; int[] second = { 1, 4, 5, 2, 4 }; bool isEqual = first.isEqual(second); Console.WriteLine(isEqual); // True } } |
That’s all about comparing arrays 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 :)