相关疑难解决方法(0)

比较两个.NET Array对象

我想比较两个.NET数组.这是一个比较字节数组的明显实现:

bool AreEqual(byte[] a, byte[] b){
    if(a.Length != b.Length)
        return false;
    for(int i = 0; i < a.Length; i++)
        if(a[i] != b[i])
            return false;

    return true;
}
Run Code Online (Sandbox Code Playgroud)

这里可以看到更精致的方法(通过谷歌).

  1. 比较两个.NET数组的最简单方法(使用较少的代码但可读)是什么?
  2. 比较两个.NET数组最有效的方法是什么?

.net arrays

25
推荐指数
3
解决办法
2万
查看次数

如何检查集合的内容(> 2)是一样的

我有一个清单.出于正当理由,我多次复制List并将其用于不同目的.在某些时候,我需要检查所有这些集合的内容是否相同.

好吧,我知道怎么做.但作为"短手"编码(linq ...)的粉丝,我想知道我是否能用最短的代码行检查这个效率.

    List<string> original, duplicate1, duplicate2, duplicate3, duplicate4
                                       = new List<string();

        //...some code.....
        bool isequal = duplicate4.sequenceequal(duplicate3) 
         && duplicate3.sequenceequal(duplicate2)
         && duplicate2.sequenceequal(duplicate1) 
         && duplicate1.sequenceequal(original);//can we do it better than this 
Run Code Online (Sandbox Code Playgroud)

UPDATE

Codeinchaos指出了我没有想到的某些语句(重复和列表顺序).虽然sequenceequal会处理重复,但列表的顺序可能是个问题.所以我改变代码如下.我需要复制列表.

List<List<string>> copy = new List<List<int>> { duplicate1, duplicate2,  
                                                 duplicate3, duplicate4 }; 
bool iseqaul  = (original.All(x => (copy.All(y => y.Remove(x))))
                                         && copy.All(n => n.Count == 0)); 
Run Code Online (Sandbox Code Playgroud)

UPDATE2

感谢Eric使用HashSet可以非常有效,如下所示.这不会覆盖重复.

List<HashSet<string>> copy2 =new List<HashSet<string>>{new HashSet<string>(duplicate1),
                                                       new HashSet<string>(duplicate2),
                                                       new HashSet<string> duplicate3),
                                                       new HashSet<string>(duplicate4)};
  HashSet<string> origninalhashset = new HashSet<string>(original);
  bool eq = copy2.All(x …
Run Code Online (Sandbox Code Playgroud)

c# linq

5
推荐指数
1
解决办法
1067
查看次数

标签 统计

.net ×1

arrays ×1

c# ×1

linq ×1