−using System;
using System.Collections.Generic;
using System.Linq;
var original = new Preset("base", new[] { "draft" });
var copy = (Preset)original.Copy();
copy.Tags.Add("reviewed");
Console.WriteLine(string.Join(",", original.Tags));
Console.WriteLine(string.Join(",", copy.Tags));
interface IPreset { IPreset Copy(); }
sealed class Preset(string name, IEnumerable<string> tags) : IPreset
{
public string Name { get; set; } = name;
public List<string> Tags { get; } = new(tags);
public IPreset Copy() => new Preset(Name, Tags);
}
+using System;
using System.Collections.Generic;
using System.Linq;
var original = new Preset("base", new[] { "draft" });
var copy = (Preset)original.Copy();
copy.Tags.Add("reviewed");
Console.WriteLine(string.Join(",", original.Tags));
Console.WriteLine(string.Join(",", copy.Tags));
// 호출부가 내부 필드를 직접 복사하지 않도록 복제 동작을 제공한다.
interface IPreset { IPreset Copy(); }
// 가변 태그 목록을 새로 만들어 원형과 복제본의 수정을 분리한다.
sealed class Preset(string name, IEnumerable<string> tags) : IPreset
{
public string Name { get; set; } = name;
public List<string> Tags { get; } = new(tags);
public IPreset Copy() => new Preset(Name, Tags);
}