title: Iterate through an array of arbitrary dimension
body: <p>Using c#, how to iterate through an multidimensional array of <em>unknown</em> dimensions?</p>
  
  <p>For example consider setting each element of the array to a specified value, all entries of the array needs to be iterated. The method should handle all the following cases and fill all entries with the value 4, regardless of the dimension of the array passed.</p>
  
  <pre><code>ClearArray(new int[3], 4);
  ClearArray(new int[3,3], 4);
  ClearArray(new int[3, 3, 3, 3], 4);
  </code></pre>
  
  <p>The methods signature obviously looks something like</p>
  
  <pre><code>static void ClearArray(Array a, int val) { ... }
  </code></pre>
  
  <p>I know how to iterate through <em>one</em> dimension:</p>
  
  <pre><code>for (int i=0; i&lt;a.GetLength(dimension); i++) 
  {
      ...
  }
  </code></pre>
  
  <p><strong>Note</strong>: This question is not about 2D arrays, 3D arrays, nor 4D arrays. It should handle whatever dimension the <code>Rank</code> property on the <code>Array</code> object says.</p>