Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're running up against the difference between a reference type and a value type. </p> <p>Here's an <a href="http://msdn.microsoft.com/en-us/library/t63sy5hs(VS.80).aspx" rel="nofollow noreferrer">msdn article</a> on differences.</p> <p>A more descriptive answer would be that both are actually performing the same operation, the difference is that in the first example (the two integers) the assignment of mVar2 = mVar1 is assigning the value of mVar1 to mVar2, which is 1. However, in the case of the DataTable, what's actually being assigned is a memory location, not a DataTable.</p> <p>Say, for instance, that the DataTable you created resides in memory location 20. That would mean that the reference mVar1 would hold a reference to that location (20). When you do the assignment mVar2 = mVar1, you're telling mVar2 to hold the same value as mVar1, so mVar2 then references to memory location 20 as well. The result being that both variables reference the same DataTable.</p> <p>In order to achieve the behavior you're describing, you would indeed need to have a Copy ability, as you've stated. It would have to allocate an entirely new object, and copy the state of the previous object to the new one.</p> <p>For the DataTable class you could extend it like this inside an extension method:</p> <pre><code>public static DataTable Copy(this DatTable original) { var result = new DataTable(); //assume Property1 was a property of a DataTable result.Property1 = original.Property1; //continue copying state from original to result return result; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload