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

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

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.

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

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

解决方法

string s = new string(foo1);
string t = new string(foo2);

int c = string.Compare(s,t);
if(c==0){
    //its equal
}

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...