c# - 如何比较两个 char[] 数组的等价性?

标签 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.

逐个字母地比较它们,但我猜还有更简单的方法。有趣的是,我为 comparing char[] for eqivalency c# 和类似关键字所做的大部分谷歌搜索都会导致关于比较 STRING 数组或在数组的 PART 匹配时比较字符串或 char 数组的信息一团糟某事或其他......我一直无法找到任何关于测试 char 数组是否相等的简单方法。遍历每个数组是最好的方法吗?或者有什么方法可以比较 foo1=foo2 还是 foo1==foo2?这些都不适合我。

基本上,我需要测试 char foo1[]char foo2[] 是否都是 {B,A,R}

最佳答案

您可能正在寻找:

foo2.SequenceEqual(foo1)

关于c# - 如何比较两个 char[] 数组的等价性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16497898/

相关文章:

compare - Gitlab 分支/标签从低版本到更新版本的比较

c# - 测试源与 2010 相比不可用

c# - 为什么抽象类要实现抽象基类的抽象方法?

c# - 将文本 SQL 与 LINQ 结合使用

javascript - react array.map()

javascript - 在javascript中按降序对日期和时间进行排序

java - 如何从 main() 访问字符串数组的元素

c - 搜索字符串并将其与字符串数组进行比较

javascript - 比较 JavaScript 中的函数参数

c# - 如何使用 Winform C# 4.5 在任务栏中显示进度