Calculations inside Foreach loop effecting the iterating list values
<p>I am encountering a rather strange problem. I am iterating over a type of List and within my loop I assign the iterator variable to another local object. Now changing anything in that local object is causing changes in the List on which loop is iterating. Let me clear this with a code sample.</p>

<pre><code>var balances = DBHelperADO.Select("select * from Orders"); 
// balances is of type List&lt;MyModel&gt;

foreach (var item in balances)
{ 
    MyModel model = new MyModel();
    model = item;
    var thisQty = details.Where(x =&gt; x.Code == item.Code).Sum(x =&gt; x.QTY);

    // details is another List&lt;MyModel&gt; holding values from the GUI

    model.BLNC = model.BLNC - thisQty;
    model.VAL = (model.BLNC == 0) ? 0 : model.VAL - (thisQty * model.RATE);
    model.TABLE = "Orders";
    toUpdate.Add(model);  // toUpdate is a List&lt;MyModel&gt;                    
}
</code></pre>

<p>Now my understanding is that the iterator variable (in this case the <strong>item</strong>) is readonly and when I write:</p>

<blockquote>
  <blockquote>
    <p>model = item;</p>
  </blockquote>
</blockquote>

<p>I am making a copy of my <strong>item</strong> values. But when I do the the calculations on the model object it causes the same changes on the balances list.</p>

<p>I am not getting why its effecting the balances list. I am performing calculations on local scoped <em>model</em> object. Then why those are reflecting on the iterating list (balances).</p>

<p>Please help me what I am doing wrong.</p>

<p>Regards</p>