✅ Required Member must be set in the object initializer or attribute constructor

new PresetDataModel(preset)


give a compile error
Severity    Code    Description    Project    File    Line    Suppression State
Error    CS9035    Required member 'PresetDataModel.Name' must be set in the object initializer or attribute constructor.


the class in question
public class PresetDataModel
{
    public PresetDataModel(ToppingPreset preset)
    {
        Id = preset.Id;
        Name = preset.Name;
        foreach(var topping in preset.Toppings)
        {
            ToppingToValuePairs.Add(new ToppingToValuePair()
            {
                ToppingId = topping.ToppingId,
                ToppingValueId = topping.ToppingValueId,
            });
        }
    }
    public Guid Id { get; set; }
    public required string Name { get; set; }
    public List<ToppingToValuePair> ToppingToValuePairs { get; set; } = new List<ToppingToValuePair>();

}


if my constructor sets it, why do I get the compile error? Is there some way to avoid besides the following
new PresetDataModel(preset) { Name = preset.Name }
Was this page helpful?