CKTIS / C#
with: practical reference
with: practical reference: with creates a nondestructive copy only for a record class, an anonymous type, or a non-void value type, including a struct or tuple.
Version: C# language; verified against C# 14 reference
Syntax
with /* grammar-specific form */with creates a nondestructive copy only for a record class, an anonymous type, or a non-void value type, including a struct or tuple.
Use it to produce the next version of a value without mutating the original.
The copy is shallow, so reference-valued members remain shared.
Required members and init rules must remain satisfied; an ordinary non-record class cannot opt in by adding a protocol.
The complete program actually uses with; its exact expected stdout is 2.
Examples
using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;
var first = new Person("Ada", 1); var second = first with { Level = 2 }; Console.WriteLine(second.Level); sealed record Person(string Name, int Level);
Result
2
The complete program actually uses `with`; its exact expected stdout is `2`.