Use initialization syntax as follows:
var contacts = new ContactList { { "Dan", " dan.tao@email.com " }, { "Eric", " ceo@google.com " } };
... I understand that my ContactList type must define an Add method that takes two string parameters
public void Add(string name, string email);
What confuses me a bit is that the initializer { } syntax seems most useful when creating read-only or fixed-size collections . After all, it is intended to mimic the initialization syntax for an array, right? (OK, so arrays are not read-only, but they are a fixed size.) And, of course, it can only be used when the contents of the collection are known (at least the number of elements) at compile time.
Thus, it would seem that the basic requirement for using the initializer syntax of this collection (having the Add method and, therefore, a mutable collection) does not correspond to the typical case in which it would be very useful.
I am sure that I did not think about this issue as a C # development team; it just seems that there may be different rules for this syntax that would better match its typical usage scenarios.
Am I really here? Is it advisable to use the { } syntax to initialize fixed-size collections not as often as I think? What other factors could affect the wording of the requirements for this syntax, which I simply donβt think about?
source share