Why is an Add method required for { } initialization?

前端 未结 9 2132
渐次进展
渐次进展 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:08

    What should the initialization syntax use, if not an Add method? The initialization syntax is 'run' after the constructor of the collection is run, and the collection fully created. There must be some way of adding items to the collection after it's been created.

    If you want to initialize a read-only collection, do it in the constructor (taking a T[] items argument or similar)

提交回复
热议问题