The question is somewhat confusing, since the question has nothing to do with LINQ, has nothing to do with general dispersion, and it also has a collection initializer as well as an object initializer. The real question is, as far as I can say, βwhy is it not legal to use a collection initializer outside an expression to create an object?β
The relevant design principle here is that, in general, we want the operations that create and initialize the objects to have the word βnewβ in them somewhere, as a signal to the reader that the creation of the object is taking place here. (Yes, there are a few exceptions to this rule in C #. As an exercise for the reader, see if you can name them all.)
Performing your actions makes it difficult to reason about code. Quickly, what does it do?
d = new List<int>() { 10, 20, 30 }; d = { 40, 50, 60 };
Does the second line add 40, 50, 60 to the existing list? Or is he replacing the old list with the new one? There is no βnewβ one, and does the reader expect that a new object has been created?
When you speak
q = new Whatever() { MyList = { 40, 50, 60 } };
which does not create a new list; it adds 40, 50, 60 to the existing list as designated by the constructor. Therefore, your proposed syntax is ambiguous and confusing as to whether a new list is created or not.
The proposed function is both confusing and unnecessary, so it is unlikely that it will be implemented in the near future.
Eric Lippert Jan 23 '11 at 16:38 2011-01-23 16:38
source share