Consider the following code:
public sealed class Order
{
public Order()
{
Items = new List<OrderItem>();
}
public List<OrderItem> Items { get; private set; }
}
public sealed class OrderItem
{
}
and here's Order
initialization in another class.
var order = new Order
{
Items =
{
new OrderItem(),
new OrderItem()
}
};
Could you explain why it works? As you see the Order
has private set
property, so I thought it would be impossible to set its value.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…