Compare values ​​from 2 C # lists

I want to compare the values ​​of two lists for the program that I am doing. I want it to compare the first value of List 1 with the first value of List 2, and then the second value of List 1 with the second value of List 2, etc.

How do I do this in C #?

+6
source share
1 answer

There is a special method called SequenceEqual :

 IList<int> myList1 = new List<int>(...); IList<int> myList2 = new List<int>(...); if (myList1.SequenceEqual(list2)) { ... } 

You can perform selective sequence comparisons using the Zip method. For example, to see if there is any pair in the difference of three, you can do this:

 IList<int> myList1 = new List<int>(...); IList<int> myList2 = new List<int>(...); if (myList1.Zip(list2, (a, b) => Math.Abs(a - b)).Any(diff => diff > 3)) { ... } 
+11
source

Source: https://habr.com/ru/post/947349/


All Articles