title: C# why is my operator overload mutating the object rather than returning a new one?
body: <p>I'm working on a matrix/vector class(es) and part of it has operator overloading. One of the odd things is, that instead of returning a new object (in this case vector) it's mutating the vector, AND returning a new one. How do I cleanly return a new instance of my Vector?</p>
  
  <p>here are the constructors:</p>
  
  <pre><code>    private List&lt;double&gt; v = new List&lt;double&gt;();
      private int dims;
  
  
      public Vector(double[] a)
      {
          foreach (double d in a)
          {
              v.Add(d);
              dims++;
          }
      }
  
      public Vector(int dims)
      {
          var a = new double[dims];
          v = a.ToList();
          this.dims = dims;
      }
  
      private Vector(List&lt;double&gt; a)
      {
          v = a;
          dims = a.Count;
      }
  </code></pre>
  
  <p>and the operator overloads (just posting addition because all of them have the problem, and have similar construction, so should have identical solutions)</p>
  
  <pre><code>    public static Vector operator + (Vector a, Vector b)
      {
          Vector c = new Vector();
          c = a;
          for (int dim = 0; dim &lt; c.dims; dim++)
          {
              c[dim] += b[dim];
          }
          return c;
      }
  </code></pre>
  
  <p>EDIT: So, I've changed the class to a struct, and it seems like I still have the issue. Maybe it''s because the variable <code>v</code> is a list, and therefore it's a class, it's still passing in the reference to the list? Maybe I have to use an array instead of a list?</p>