Passing Collections Immutably
<p>I'm doing a big refactor of a pile of code that used to use a bunch of multidimensional arrays that were constantly getting resized. I created a data object to replace the 2D array, and I'm now passing a list of these around.</p>

<p>I discovered something that worries me a little though. Let's say I have some code that looks like this:</p>

<pre><code>List&lt;NCPoint&gt; basePoints = new List&lt;NCPoint&gt;();

// ... snip populating basePoints with starting data

List&lt;NCPoint&gt; newPoints = TransformPoints(basePoints, 1, 2, 3);

public List&lt;NCPoint&gt; TransformPoints(List&lt;NCPoint&gt; points, int foo, int bar, int baz){
    foreach(NCPoint p in points){
        points.X += foo
        points.Y += bar
        points.Z += baz
    }

    return points;
}
</code></pre>

<p>The idea is to keep a list of the original points (<code>basePoints</code>) and a list of the updated points (<code>newPoints</code>). But C# passes the list by reference, as with any object. This updates <code>basePoints</code> in place, so now both <code>basePoints</code> and <code>newPoints</code> will have the same data.</p>

<p>At the moment, I'm trying to be careful about making a full copy of the passed-in <code>List</code> before I muck with the data. Is that the only sensible way to make sure changes to an object within a function don't have side effects outside the function? Is there anything akin to passing an object with <code>const</code>?</p>
