title: When is a C# value/object copied and when is its reference copied?
body: <p>I keep getting the same issue over and over again where an object I want to reference is copied or where an object I want to copy is referenced. This happens when I use the = operator.</p>
  
  <p>For example, if I am sending the object to another form, ie: </p>
  
  <pre><code>SomeForm myForm = new SomeForm();
  SomeObject myObject = new SomeObject();
  myForm.formObject = myObject;
  </code></pre>
  
  <p>...and then modify the object in the form, the original object does not get modified. It is as if the object was copied and not referenced. Yet, when I do this:</p>
  
  <pre><code>SomeObject myObject = new SomeObject();
  SomeObject anotherObject = new SomeObject();
  anotherObject = myObject;
  </code></pre>
  
  <p>...and then modify <code>anotherObject</code>, <code>myObject</code> gets modified as well.</p>
  
  <p>The most aggravating case is when I try to Clone one of my defined objects:</p>
  
  <pre><code>public class SomeObject
  {
      double value1, value2;
  
      //default constructor here
  
      public SomeObject(val1, val2)
      {
          value1 = val1;
          value2 = val2;
      }
  
      public void Clone(SomeObject thingToCopy)
      {
          this.value1 = thingToCopy.value1;
          this.value2 = thingToCopy.value2;
      }
  }
  </code></pre>
  
  <p>when I do this...</p>
  
  <pre><code>SomeObject obj1 = new SomeObject(1, 2);
  SomeObject obj2 = new SomeObject();
  obj2.Clone(obj1);
  </code></pre>
  
  <p>...<code>obj1</code> is referenced and any modifications to <code>obj2</code> changes <code>obj1</code>.</p>
  
  <p>System objects such as <code>int, double, string</code>, etc seem to always be copied, except for in the case of the clone method above.</p>
  
  <p>My question is, not taking into account the use of the <code>ref</code> keyword in functions, when does an object get copied and when does an object get referenced in every case of the matter (i.e. when passing to functions, when setting as other objects (like the first two above examples), when copying member variables like the third example, etc.)?</p>