Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Compare two char[] arrays for equivalency?

Tags:

arrays

c#

compare

Right now, I have two char arrays, foo1[] and foo2[]. When I convert them to string and output to the console, they BOTH appear as bar. I know that I can do something like this:

int g;
for (int i=0;i<length.foo1;i++) {      // loop from 0 to length of array
    if (foo1[i]=foo2[i]) {             // if foo1[0] and foo2[0] are the same char
    g++;                               // increment a counter by 1
}
else                                   // otherwise...
{
    i=100;                             // set i to something that will halt the loop
}
if (g = length.foo1) {                 // if the incremented counter = length of array
    return true;                       // arrays are equal, since g only increments
}                                      // in the case of a match, g can ONLY equal the
else {                                 // array length if ALL chars match
    return false;                      // and if not true, false.

to compare them letter by letter, but I'm guessing there's an easier way. Interestingly, most of the googling I do for comparing char[] for eqivalency c# and similar keywords results in a whole mess of info about comparing STRING arrays, or comparing string or char arrays when PART of the array matches something or another...I've not been able to find anything about easy ways of testing char arrays as being equal. Is stepping through each array the best way? or is there some way to compare if foo1=foo2 or foo1==foo2? Neither of those worked for me.

Basically, I need to test if char foo1[] and char foo2[] are BOTH {B,A,R}

like image 901
dwwilson66 Avatar asked Nov 29 '22 01:11

dwwilson66


1 Answers

You are possibly looking for:

foo2.SequenceEqual(foo1)
like image 78
Ken Kin Avatar answered Dec 07 '22 00:12

Ken Kin