How to compare arrays in C #?

Possible duplicate:
The easiest way to compare arrays in C #

How to compare two arrays in C #?

I am using the following code, but its result is false. I expected this to be true.

Array.Equals(childe1,grandFatherNode); 
+71
arrays c # compare
Dec 12 2018-10-12
source share
6 answers

You can use Enumerable.SequenceEqual () in System.Linq to compare contents in an array

 bool isEqual = Enumerable.SequenceEqual(target1, target2); 
+152
Feb 10 2018-12-12T00:
source share

You are comparing object references and they are not the same. You need to compare the contents of the array.

.NET2 Solution

The option iterates through the elements of the array and calls Equals() for each element. Remember that you need to override the Equals() method for array elements if they are not the same object reference.

An alternative is to use this common method to compare two common arrays:

 static bool ArraysEqual<T>(T[] a1, T[] a2) { if (ReferenceEquals(a1, a2)) return true; if (a1 == null || a2 == null) return false; if (a1.Length != a2.Length) return false; EqualityComparer<T> comparer = EqualityComparer<T>.Default; for (int i = 0; i < a1.Length; i++) { if (!comparer.Equals(a1[i], a2[i])) return false; } return true; } 

.NET 3.5 or higher solution

Or use SequenceEqual if Linq is available to you (.NET Framework> = 3.5)

+32
Dec 12 2018-10-12
source share

There is no static Equals method in the Array class, so you are actually using Object.Equals , which determines whether these two objects refer to the same object.

If you want to check if arrays contain the same elements in the same order, you can use the SequenceEquals extension method:

 childe1.SequenceEqual(grandFatherNode) 

Edit:

To use SequenceEquals with multidimensional arrays, you can use the extension to list them. Here is the extension to enumerate a two-dimensional array:

 public static IEnumerable<T> Flatten<T>(this T[,] items) { for (int i = 0; i < items.GetLength(0); i++) for (int j = 0; j < items.GetLength(1); j++) yield return items[i, j]; } 

Using:

 childe1.Flatten().SequenceEqual(grandFatherNode.Flatten()) 

If your array has more sizes than two, you will need an extension that supports this number of dimensions. If the number of measurements changes, you will need a slightly more complex code to cyclically change the number of measurements.

First you need to make sure that the number of dimensions and the size size of the arrays are the same before comparing the contents of the arrays.

Edit 2:

Turns out you can use the OfType<T> method to align the array, as RobertS noted. Naturally, this only works if all the elements can be cast to the same type, but this is usually the case if you can compare them. Example:

 childe1.OfType<Person>().SequenceEqual(grandFatherNode.OfType<Person>()) 
+15
Dec 12 2018-10-12
source share

Array.Equals compares links, not their contents:

Currently, when you compare two arrays with the = operator, we really use the System.Object = operator, which only compares instances. (i.e. this uses referential equality, so it will only be true if both arrays point to the same instance)

Source

If you want to compare the contents of arrays, you need to loop around the arrays and compare the elements.

The same blog has an example of how to do this.

+5
Dec 12 '10 at 18:50
source share

The Equals method performs a reference comparison β€” if the arrays are different objects, this will indeed return false.

To check if the arrays contain the same values ​​(and in the same order), you will need to iterate over them and check the equality for each.

+1
Dec 12 2018-10-12
source share

Array.Equals () seems to only check for the same instances.

There is no method for comparing values, but it would be very easy to write.

Just compare the lengths, if not equal, return false. Otherwise, swipe through each value in the array and determine if they match.

0
Dec 12 2018-10-12
source share



All Articles