title: double[,] vs. List<List<double>>
body: <p>I have code for a matrix multiplication lab using double[,] and I wanted to compare it with an implementation using a List&lt; List&lt; double>></p>
  
  <pre><code>public static Matrix operator *(Matrix a, Matrix b)
      {
          if (a.Width != b.Height)
          {
              throw new InvalidOperationException();
          }
  
          double[,] result = new double[a.Height, b.Width];
  
          for (int i = 0; i &lt; a.Height; i++)
          {
              for (int j = 0; j &lt; b.Width; j++)
              {
                  for (int k = 0; k &lt; a.Width; k++)
                      result[i, j] += a[i, k] * b[k, j];
              }
          }
  
          return new Matrix(result);
      }
  </code></pre>
  
  <p>'result' here has the right data:</p>
  
  <p>Input matrix A:</p>
  
  <p>1.000 2.000 3.000 1.000</p>
  
  <p>2.000 3.000 3.000 1.000</p>
  
  <p>Input matrix B:</p>
  
  <p>1.000 0.000 0.000 0.000</p>
  
  <p>0.000 1.000 0.000 0.000</p>
  
  <p>0.000 0.000 1.000 0.000</p>
  
  <p>2.000 3.000 0.000 1.000</p>
  
  <p>Matrix product A*B</p>
  
  <p>3.000 5.000 3.000 1.000</p>
  
  <p>4.000 6.000 3.000 1.000</p>
  
  <p>Changing it to Lists...</p>
  
  <pre><code>public List&lt;List&lt;double&gt;&gt; matrix;
  
  public double this[int x, int y]
      {
          get { return matrix[x][y]; }
          set { matrix[x][y] = value; }
      } 
  
  public static Matrix operator *(Matrix a, Matrix b)
      {
          if (a.Width != b.Height)
          {
              throw new InvalidOperationException();
          }
  
          Matrix result = new Matrix(a.Height, b.Width);
  
          for (int i = 0; i &lt; a.Height; i++)
          {
              for (int j = 0; j &lt; b.Width; j++)
              {
                  for (int k = 0; k &lt; a.Width; k++)
                      result[i, j] += a[i, k] * b[k, j];
              }
          }
  
          return result;
      }
  </code></pre>
  
  <p>Using the same data the result is now:</p>
  
  <p>[7] [11] [6] [2]</p>
  
  <p>[7] [11] [6] [2]</p>
  
  <p>EDIT: constructor is:</p>
  
  <pre><code>public Matrix(int height, int width)
      {
          List&lt;List&lt;double&gt;&gt; points = new List&lt;List&lt;double&gt;&gt;();
  
          List&lt;double&gt; row = new List&lt;double&gt;();
  
          for (int i = 0; i &lt; width; i++)
          {
              row.Add(0.0d);
          }
  
          for (int i = 0; i &lt; height; i++)
          {
              points.Add(row);
          }
  
          matrix = points;
      }
  </code></pre>
  
  <p>it looks like it works fine, everythign is initialized to 0.0</p>
  
  <p>My question would be why does the math change between the 2 ways of storing the values.</p>