primary constructors; thus I have a class like this:
public class InventoryItem(IEntity entity, long amount = 0)
{
public required IEntity Entity { get; set; } = entity;
public long Amount { get; set; } = amount;
}
That all seems valid, however when I try to call it like this:
List<InventoryItem> inventoryItems = [];
inventoryItems.Add(new InventoryItem(entity, amount));
I get
Required member 'InventoryItem.Entity' must be set in the object initializer or attribute constructor.CS9035
Any suggestions? Can I perhaps not use primary constructors with required fields?
Maybe you need [SetsRequiredMembers]
I tried this, but Attribute 'System.Diagnostics.CodeAnalysis.SetsRequiredMembers' is not valid on this declaration type. It is only valid on 'constructor' declarations.CS0592 😁
Обсуждают сегодня