CKTIS / C#
with: odniesienie praktyczne
with: odniesienie praktyczne: with tworzy niedestrukcyjną kopię wyłącznie dla record class, typu anonimowego albo wartościowego typu innego niż void, w tym struct lub tuple.
Wersja: C# language; verified against C# 14 reference
Składnia
with /* grammar-specific form */with tworzy niedestrukcyjną kopię wyłącznie dla record class, typu anonimowego albo wartościowego typu innego niż void, w tym struct lub tuple.
Stosuj do kolejnej wersji wartości bez mutowania oryginału.
Kopia jest płytka; referencyjne składowe są współdzielone.
Wymagane members i init muszą pozostawać poprawne; zwykła class niebędąca record nie może dołączyć do with przez dodanie protokołu.
Kompletny program rzeczywiście używa with; oczekiwany stdout to dokładnie 2.
Przykłady
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);
Wynik
2
Kompletny program rzeczywiście używa `with`; oczekiwany stdout to dokładnie `2`.