Testing equality of arrays in C#

Related searches

I have two arrays. For example:

int[] Array1 = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
int[] Array2 = new[] {9, 1, 4, 5, 2, 3, 6, 7, 8};

What is the best way to determine if they have the same elements?

By using LINQ you can implement it expressively and performant:

var q = from a in ar1
        join b in ar2 on a equals b
        select a;

bool equals = ar1.Length == ar2.Length && q.Count() == ar1.Length;

Testing equality of arrays in C#, An array is a group of like-typed variables that are referred to by a common name​. And each data item is called an element of the array. Equals(Object) method  Next, the SequenceEqual method is used on the arrays: array1 is tested against array2, array3, and array4.Array. Info: The variable array1 was determined not to be equal to array2. But Array1 was equal to array3. And: Array1 was equal to array4 when the IEqualityComparer implementation (StringComparer.OrdinalIgnoreCase) was specified.

You could also use SequenceEqual, provided the IEnumerable objects are sorted first.

int[] a1 = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };    
int[] a2 = new[] { 9, 1, 4, 5, 2, 3, 6, 7, 8 };    

bool equals = a1.OrderBy(a => a).SequenceEqual(a2.OrderBy(a => a));

C#, Are two sequences the same? With the SequenceEqual extension from System.​Linq in the C# language, you can test 2 collections for equality in one statement. If the current instance is a value type, the Equals(Object) method tests for value equality. Value equality means the following: The two objects are of the same type a Byte object that has a value of 12 does not equal an Int32 object that has a value of 12, because the two objects have different run-time types.

Will the values always be unique? If so, how about (after checking equal length):

var set = new HashSet<int>(array1);
bool allThere = array2.All(set.Contains);

C# SequenceEqual Method (If Two Arrays Are Equal), Two arrays will be called equal if both the arrays have same corresponding elements and same number of elements. Comparing each element of  Equality comparisons (C# Programming Guide) It is sometimes necessary to compare two values for equality. In some cases, you are testing for value equality, also known as equivalence, which means that the values that are contained by the two variables are equal.

var shared = arr1.Intersect(arr2);
bool equals = arr1.Length == arr2.Length && shared.Count() == arr1.Length;

How to compare two arrays in C# if they are Equal or Not., From C# 3.5 onward LINQ is available and you can replace the call to Equals with a If both arrays are non-null but have different lengths, they are not equal. AreEqual(Object, Object, String, Object[]) Tests whether the specified objects are equal and throws an exception if the two objects are not equal. Different numeric types are treated as unequal even if the logical values are equal. 42L is not equal to 42.

Use extension methods (which are new in 3.0). If the length of the Intersection of the two arrays equals that of their Union then the arrays are equal.

bool equals = arrayA.Intersect(arrayB).Count() == arrayA.Union(arrayB).Count()

Succinct.

Equals on collections - C# queries, Is there an easy way to check in a unit test that two arrays are equal (that is, have the same number of elements, and each element is the same?). In Java, I would use assertArrayEquals (foo, bar);, but there seems to be no equivalent for C#.

Comparing two arrays in unit test throwing a AssertFailedException. Ask Question This will compare array elements. Browse other questions tagged c# unit

How can I compare two arrays in C#? I use the following code, but its result is false. I was expecting it to be true. Could you give us an example? – Ani Dec 12 '10 at 18:48. You can use the Enumerable.SequenceEqual () in the System.Linq to compare the contents in the array. AMing solution handle null cases (and is more 'symmetric').

For complex object don't forget that you need an Equal+GetHasCode for this to work or give the Equal method a custom EqulityComparer – maracuja-juice May 8 '18 at 14:28 1 xUnit Equal method returns false for two IEnumerables with equal content.

Comments
  • Are you actually dealing with numbers or is that just for the example?
  • Can you use a List<T> instead (already has Contains method)?
  • @ed it wasn't about a simple contains, but determining both array has the same elements, re-read the question and see the answers :)
  • @Simucal I just used integers for the example here . IN my scenario, it could be an array of objects
  • possible duplicate of Comparing two collections for equality irrespective of the order of items in them
  • Thanks for this code snippet. Works like a charm for my scenario!!
  • I find this to be much slower than just doing a loop and comparing each item. The loop may not be as nice looking, but much, much faster.
  • I'm not sure about that. As far as I know LINQ-to-Objects generate intermediate hash tables to loop over joins which are much faster than a straight loop.
  • Here is my reference: stackoverflow.com/questions/1561622/…
  • This doesn't work when both of the arrays have duplicate values. If both arrays contain {2,2}, the join will have 4 elements instead of 2 and the expression will be false instead of true.
  • yep, sort/order then SequenceEqual
  • marc , I could also compare via IStructuralEquatable (tuples and arrays). So when should I choose IStructuralEquatable vs SequenceEqual ?
  • That link describes SequenceEqual (in .NET 3.5), and will return false on this data since they are in a different order.
  • For reference, it would be (using extension methods) bool areEqual = array1.SequenceEqual(array2);
  • That looks like a method from NUnit and is not a general solution.