Show / Hide Table of Contents

with Expression

with Operator provides nondestructive mutation of the record using copy-on-write approach. .NEXT Metaprogramming provides special expression type to support the operator:

using DotNext.Linq.Expressions;
using System.Linq.Expressions;

record class MyRecord(int X);

Expression x = new MyRecord(42).Const();
Expression mutation = x.With(new()
{
    { "X", 52.Quoted }
});

With extension method generates the expression equivalent to the following C# expression:

MyRecord mutation = x with { X = 52 };

Mutation expression supports record classes and record value types.

  • Edit this page
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾