title: Are DateTimes passed by reference in C#? If not, why is my object updating as I change a variable?
body: <p>I have ran into a problem which I simply cannot get my head around. When I debug the program, I can see the program works fine - this is the strange part.</p>
  <p>The issue I am facing is when I append to a List with the new object - it seems to completely change. Let me explain better by showing my code.</p>
  <pre><code>        System.DateTime timeNow = System.DateTime.Now;
  
          List&lt;Train&gt; trainsOnNet = new List&lt;Train&gt;();
  
          for(int i = 1; i &lt;= 3; i++)
          {
              Train t = new Train();
  
              t.NewCarrige(true, &quot;A&quot;[0]);
              t.NewCarrige(false, &quot;B&quot;[0]);
  
              t.addStation(App.Stations.FirstOrDefault(x =&gt; x.GetShortCode().Equals(&quot;MTG&quot;)));
              t.addStation(App.Stations.FirstOrDefault(x =&gt; x.GetShortCode().Equals(&quot;BNS&quot;)));
              t.addStation(App.Stations.FirstOrDefault(x =&gt; x.GetShortCode().Equals(&quot;BSH&quot;)));
  
              int minsToAdd = 5;
  
              t.GetStations().ForEach(x =&gt;
              {
                  timeNow = timeNow.AddMinutes(minsToAdd);
                  x.SetArrivalTime(timeNow);
                  minsToAdd += 10;
              });
  
              timeNow = timeNow.AddMinutes(15);
  
              trainsOnNet.Add(t);
  
          }
  </code></pre>
  <p>When I add <code>t</code> to the <code>trainsOnNet</code> List, the time seems to change to the last time that the loop will generate, even before it generates.</p>
  <p>If I place a stopper on this line, I can see that the <code>t</code> instance has the correct time variables (ie, 5 minutes from the current execution time and then 10 minutes between each). However, when I then press continue and inspect the <code>trainsOnNet</code> list. This time has been changed to the next trains set of times.</p>
  <p>It appears to me that <code>timeNow</code> is being passed by reference, and as the loop increases the time, the stored time updates until I am left with 3 trains all saying the same time.</p>
  <p>For example, if I execute the program at 1951 with a stopper on <code>trainsOnNet.Add(t)</code> I can see that <code>t</code> holds 3 stations, in which the first stations Arrival time is 19:56 and the second is 20:11 and 20:26. Perfect. I then hit continue and inspect the newly appended object to my list. On inspection, the <code>t</code> instance arrival time properties have now changed to:</p>
  <p>20:56, 21:11, 21:36</p>
  <p>Doing the maths of my code, the next train should arrive 20 minutes after the previous train has arrived at the end station. Which brings me to 20:46. Thus deeming it more confusing why the first train is being changed past the second trains expected time let alone being updated without stating to do so.</p>
  <p>Any ideas would be greatly appricated.</p>
  <p>Stop on the line on the first execution:
  <a href="https://i.stack.imgur.com/zhh2U.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zhh2U.png" alt="enter image description here" /></a></p>
  <p>Stop on same line, after pressing continue (changed properties):
  <a href="https://i.stack.imgur.com/R6obZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/R6obZ.png" alt="enter image description here" /></a></p>
  <p>As you can see, the values are being changed? In this case, by a whole hour?</p>