General purpose immutable classes in C#
<p>I am writing code in a functional style in C#. Many of my classes are immutable with methods for returning a modified copy of an instance. </p>

<p>For example: </p>

<pre><code>sealed class A
{
    readonly X x;
    readonly Y y;

    public class A(X x, Y y)
    {
        this.x = x;
        this.y = y;
    }

    public A SetX(X nextX)
    {
        return new A(nextX, y);
    }

    public A SetY(Y nextY)
    {
        return new A(x, nextY);
    }
}
</code></pre>

<p>This is a trivial example, but imagine a much bigger class, with many more members. </p>

<p>The problem is that constructing these modified copies is very verbose. Most of the methods only change one value, but I have to pass <em>all</em> of the unchanged values into the constructor. </p>

<p>Is there a pattern or technique to avoid all of this boiler-plate when constructing immutable classes with modifier methods? </p>

<p>Note: I do not want to use a <code>struct</code> for <a href="https://stackoverflow.com/questions/7086879/immutable-class-vs-immutable-struct">reasons discussed elsewhere on this site</a>. </p>

<hr>

<p>Update: I have since discovered this is called a "copy and update record expression" in F#. </p>