There is one array List<Object1> objects1; . It is filled with elements.

There is also another array List<Object1> objects2; . It is also filled with elements.

In the program, you need to check whether the elements are the same in both arrays. I am doing objects1.Equals(objects2) .

Always false

I think that, possibly, the List array itself is a reference type, and then it turns out that the elements of the arrays never match.

I ask for help from more experienced anons: how to check whether the elements of both arrays are the same?

  • Offtopic, but after solving the problem, check here.stackoverflow.com/questions/473095/… - A1essandro
  • @ A1essandro, I understand everything. I call the list an array to simplify the issue - eriksongerson
  • Is it necessary to take into account that the elements in the second list may be the same, but go in a different order? Can there be several identical elements in the list? - Andrei NOP
  • @AndreyNOP, yes, you need. The list can not have several identical values - eriksongerson
  • In this case, the marked answer does not suit you; it compares the order in that number, for example, Console.WriteLine(new[]{ 1, 2, 3 }.SequenceEqual(new[]{ 3, 2, 1 })); will give out False - Andrey NOP

3 answers 3

Try

  var eq = objects1.SequenceEqual(objects2); 
  • I may be doing something wrong, but the SequenceEqual() - eriksongerson method is not applied to my arrays
  • one
    You need to register using System.Linq; . Then this method will be available to you. - A1essandro
  • I tested and SequenceEqual (), in my opinion it applies only to standard types (it works on List <int>), but not on List <MyClass> anymore - TEA
  • Perhaps this method works well with transfers - TEA
  • Everything is working. I had to dig a little and take part of the code from msdn.microsoft.com/en-us/library/bb342073.aspx@tym32167, thanks - eriksongerson

As far as I remember, Equals compares the storage address in memory for non-base types.

For it to work for the lists, you need to override it, or there are 2 options

write a function with a cycle where all the elements are compared with each and so on, but you have to tinker with this tk 2 of the same list may contain elements in a different order, then you need to sort them before comparing

    Given that the order of the elements in the collections may differ and there are no duplicate elements in the collections, the following code will do:

     int[] array1 = { 1, 2, 3 }; int[] array2 = { 3, 2, 1 }; bool eq = !array1.Except(array2).Any() && !array2.Except(array1).Any(); Console.WriteLine(eq); 

    those. we select all the elements from the first collection, which are not in the second and vice versa, and then we see that if there is not a single element in the output, then the collections contain only identical elements.

    Or this:

     bool eq = array1.Count() == array2.Count() && array1.Intersect(array2).Count() == array1.Count(); 

    Those. there are the same number of elements in the collections and the same number of elements in their intersection; this option will work faster when there are different number of elements in the collections.