title: Multiplication operator overload modifying original variable (C#)?
body: <p>I'm working on a matrices library as a learning project, and in doing so I've overloaded the * operator to perform matrix multiplication. I've also overloaded it to handle scalar multiplication (multiplying every element of the matrix by a double). My problem is that when the following code is run, both test2 and test are being modified, which is undesired. </p>
  
  <pre><code>Matrix test2 = 2 * test;
  </code></pre>
  
  <p>I'm sure this is a problem with the fact that Matrix is a class, and is therefore being passed by reference, but without changing Matrix to a struct (something I don't think will be suitable, and when tried doesn't seem to work), I can't see any way of resolving this. How would I fix this?</p>
  
  <p>The actual operator code is</p>
  
  <pre><code>public static Matrix operator *(Matrix m1, double c)
  {
     Matrix ret = m1;
  
     for (long i = 0; i &lt; ret.Width; i++) // Iterate over the rows.
      {
          for (long p = 0; p &lt; ret.Height; p++) // Iterate over the columns.
          {
              ret[i, p] *= c;
          }
      }
  
     return ret;
  }
  </code></pre>