/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/csharp/language-reference/operators/snippets/with-expression/UserDefinedCopyConstructor.cs
29 строк
825 B
Petr Kulikov
C# 9 reference update: a with expression (#21392)
09 ноя 2020, 18:21
Не верифицирован
09 ноя 2020, 18:21
b70dd2b
Код
Авторство
О чём код?
using System; using System.Collections.Generic; public class UserDefinedCopyConstructorExample { public record TaggedNumber(int Number, List<string> Tags) { protected TaggedNumber(TaggedNumber original) { Number = original.Number; Tags = new List<string>(original.Tags); } public string PrintTags() => string.Join(", ", Tags); } public static void Main() { var original = new TaggedNumber(1, new List<string> { "A", "B" }); var copy = original with { Number = 2 }; Console.WriteLine($"Tags of {nameof(copy)}: {copy.PrintTags()}"); // output: Tags of copy: A, B original.Tags.Add("C"); Console.WriteLine($"Tags of {nameof(copy)}: {copy.PrintTags()}"); // output: Tags of copy: A, B } }