title: Comparing arrays in C#
body: <p>I am trying to compare two arrays with each other. I tried this code and got the following errors.</p>
  
  <pre><code>static bool ArraysEqual(Array a1, Array a2)
  {
      if (a1 == a2)
          return true;
  
      if (a1 == null || a2 == null)
          return false;
  
      if (a1.Length != a2.Length)
          return false;
  
      IList list1 = a1, list2 = a2; //error CS0305: Using the generic type 'System.Collections.Generic.IList&lt;T&gt;' requires '1' type arguments
      for (int i = 0; i &lt; a1.Length; i++)
      {
          if (!Object.Equals(list1[i], list2[i])) //error CS0021: Cannot apply indexing with [] to an expression of type 'IList'(x2)
              return false;
      }
      return true;
  }
  </code></pre>
  
  <p>Why do I get that error? I went for a low-tech solution and did this which works fine, but I need to copy/paste it several times for each type.</p>
  
  <pre><code>static bool ArraysEqual(byte[] a1, byte[] a2)
  {
      if (a1 == a2)
          return true;
  
      if (a1 == null || a2 == null)
          return false;
  
      if (a1.Length != a2.Length)
          return false;
  
      for (int i = 0; i &lt; a1.Length; i++)
      {
          if (a1[i] != a2[i])
              return false;
      }
      return true;
  }
  </code></pre>