如何比较两个char []数组的等效性?

dww*_*n66 3 c# arrays compare

现在,我有两个char数组,foo1[]foo2[].当我将它们转换为字符串并输出到控制台时,它们同时显示为bar.我知道我可以这样做:

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.
Run Code Online (Sandbox Code Playgroud)

逐字逐句比较,但我猜这是一种更简单的方法.有趣的是,我所做的大部分谷歌搜索comparing char[] for eqivalency c#和类似的关键字都导致了大量关于比较STRING数组的信息,或者当数组的部分匹配某些东西时比较字符串或字符数组......我一直无法找到任何关于测试char数组的简单方法都是平等的.踩过每个阵列是最好的方法吗?或者有没有办法比较foo1 = foo2或foo1 == foo2?这些都不适合我.

基本上,我需要测试是否char foo1[]char foo2[]两者兼而有之{B,A,R}

Ken*_*Kin 15

您可能正在寻找:

foo2.SequenceEqual(foo1)
Run Code Online (Sandbox Code Playgroud)