C# 检查两个字节数组的相等性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18472867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:19:03  来源:igfitidea点击:

Checking equality for two byte arrays

c#

提问by Masriyah

I am checking the equality of two byte arrays, and I wanted some help because what I have returns false even though the arrays should be equal.

我正在检查两个字节数组的相等性,我需要一些帮助,因为即使数组应该相等,我也返回 false。

Within my debug I could see both of a1 and b1 are equal, but it is not going inside the while loop to increment i.

在我的调试中,我可以看到 a1 和 b1 相等,但它不会进入 while 循环以增加 i。

public bool Equality(byte[] a1, byte[] b1)
{
    int i;
    bool bEqual;
    if (a1.Length == b1.Length)
    {
        i = 0;
        while ((i < a1.Length) && (a1[i]==b1[i]))
        {
            i++;
        }

        if (i == a1.Length)
        {
            bEqual = true;
        }
    }
    return bEqual;
}

This always returns false: (a1[i]==b1[i]).

这总是返回 false: (a1[i]==b1[i])

采纳答案by p.s.w.g

You need to add a return value somewhere. This should work:

您需要在某处添加一个返回值。这应该有效:

public bool Equality(byte[] a1, byte[] b1)
{
   int i;
   if (a1.Length == b1.Length)
   {
      i = 0;
      while (i < a1.Length && (a1[i]==b1[i])) //Earlier it was a1[i]!=b1[i]
      {
          i++;
      }
      if (i == a1.Length)
      {
          return true;
      }
   }

   return false;
}

But this is much simpler:

但这要简单得多:

return a1.SequenceEqual(b1);

Alternatively, you could use IStructuralEquatablefrom .NET 4:

或者,您可以IStructuralEquatable从 .NET 4使用:

return ((IStructuralEquatable)a1).Equals(b1, StructuralComparisons.StructuralEqualityComparer)


If performance is a concern, I'd recommend rewriting your code to use the Binaryclass, which is specifically optimized for this kind of use case:

如果性能是一个问题,我建议重写您的代码以使用Binary该类,该类专门针对此类用例进行了优化:

public bool Equality(Binary a1, Binary b1)
{
    return a1.Equals(b1);
}

A quick benchmark on my machine gives the following stats:

我的机器上的快速基准测试给出了以下统计数据:

Method                   Min         Max         Avg
binary equal:          0.868       3.076       0.933    (best)
for loop:              2.636      10.004       3.065
sequence equal:        8.940      30.124      10.258
structure equal:     155.644     381.052     170.693

Download this LINQPad fileto run the benchmark yourself.

下载此 LINQPad 文件以自己运行基准测试。

回答by Moop

This should work:

这应该有效:

public bool Equality(byte[] a1, byte[] b1)
{
   if(a1 == null || b1 == null)
       return false;
   int length = a1.Length;
   if(b1.Length != length)
      return false;
   while(length >0) {
       length--;
       if(a1[length] != b1[length])
          return false;           
   }
   return true;        
}

回答by Sam I am says Reinstate Monica

You should add some return statements:

您应该添加一些返回语句:

public bool Equality(byte[] a1, byte[] b1)
{
    int i = 0;
    if (a1.Length == b1.Length)
    {
        while ((i < a1.Length) && (a1[i]==b1[i]))
        {
            i++;
        }
    }
    return i == a1.Length;
}

Or, better yet

或者,更好

public bool Equality(byte[] a1, byte[] b1)
{
    if(a1.Length != b1.Length)
    {
        return false;
    }

    for (int i = 0; i < a1.Length; i++)
    {
        if (a1[i] != b1[i])
        {
            return false;
        }
    }
    return true;
}

回答by Magnus

To check equality you can just write:

要检查相等性,您可以只写:

var areEqual =  a1.SequenceEqual(b1);

回答by Haney

I'd recommend some short-circuiting to make things a bit simpler, and use of object.ReferenceEqualsto short-circuit for cases when the arrays are the same reference (a1 = b1):

我建议使用一些短路来使事情变得更简单,并object.ReferenceEquals在数组具有相同引用 ( a1 = b1) 的情况下使用来短路:

public bool Equality(byte[] a1, byte[] b1)
{
    // If not same length, done
    if (a1.Length != b1.Length)
    {
        return false;
    }

    // If they are the same object, done
    if (object.ReferenceEquals(a1,b1))
    {
        return true;
    }

    // Loop all values and compare
    for (int i = 0; i < a1.Length; i++)
    {
        if (a1[i] != b1[i])
        {
            return false;
        }
    }

    // If we got here, equal
    return true;
}