Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two arrays in c# and find the exact match of arrays

Tags:

c#

c#-4.0

I want to compare two array in c#. If they are matched go to if condition else go to the else condition. How can we do that in c#.

int[] numbers = new int[] { 1, 2, 3, 4, 5 };
int[] numbers2 = new int[] { 1, 2, 3, 4, 5 };

I want to compare the two arrays like

if(numbers == numbers2){
   do something
}else{
   do something
}
like image 324
Jonathan Avatar asked Oct 27 '25 20:10

Jonathan


1 Answers

You can use the Enumerable.SequenceEqual() extension method. It does exactly what you want:

if (numbers.SequenceEqual(numbers2)) {
    // do something
} else {
    // do something else
}
like image 187
Alexander Taran Avatar answered Oct 30 '25 11:10

Alexander Taran