Compare two bytes buffers

Comparing two arrays.

What doesn’t work

   int[] a = new int[2]{1,2};
   int[] b = new int[2]{1,2};

   if (a.Equals(b))
   {
      // No match. The Array objects differ although they contain the same value.
   }
   
   b = a;
   
   if (a.Equals(b))
   {
      // Match, the Array objects are the same.
   }

What does work

Strangely enough the Array function does not contain a compare function.

To compare two buffers use the following code

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

Note this code is optimized by the compiler which detects 0 < Length loops.

Lastest update in May 2011, inital post in May 2011

Write a comment

I appreciate comments, suggestions, compliments etc. Unfortunately I have no time to reply to them. Useful input will be used for sure!