Why is an Add method required for { } initialization?

前端 未结 9 2130
渐次进展
渐次进展 2021-02-10 18:54

To use initialization syntax like this:

var contacts = new ContactList
{
    { \"Dan\", \"dan.tao@email.com\" },
    { \"Eric\", \"ceo@google.com\" }
};
<         


        
9条回答
  •  深忆病人
    2021-02-10 19:17

    Collection initializers are expressions, so they can be used where only expression are valid, such as a field initializer or LINQ query. This makes their existence very useful.

    I also think the curly-bracketed { } kind of initialization, smells more like a fixed size collection, but it's just a syntax choice.

提交回复
热议问题