CKTIS / C#
with: praktická referencia
with: praktická referencia: with vytvorí nedeštruktívnu kópiu iba pre record class, anonymný typ alebo hodnotový typ iný než void, vrátane struct či tuple.
Verzia: C# language; verified against C# 14 reference
Syntax
with /* grammar-specific form */with vytvorí nedeštruktívnu kópiu iba pre record class, anonymný typ alebo hodnotový typ iný než void, vrátane struct či tuple.
Použite ho na vytvorenie ďalšej verzie hodnoty bez zmeny originálu.
Kópia je plytká, takže členy s referenčnou hodnotou zostávajú zdieľané.
Required members a pravidlá init musia zostať splnené; bežná trieda, ktorá nie je record, sa nemôže zapojiť pridaním protokolu.
Úplný program skutočne používa with; presne očakávaný stdout je 2.
Príklady
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);
Výsledok
2
Úplný program skutočne používa `with`; presne očakávaný stdout je `2`.