Having program with such code :
var subtree = new Tree<int>(5, EnumeratorOrder.BreadthFirstSearch) { 1, 2 };
var tree = new Tree<int>(7, EnumeratorOrder.BreadthFirstSearch) { subtree, 10, 15 };
I сan't understan what means { 1, 2 }?
I сan't understan what means { 1, 2 }
The {1, 2} are Collection Initializers.
They represent a short-hand version of
var temp = new Tree<int>(5, EnumeratorOrder.BreadthFirstSearch);
temp.Add(1);
temp.Add(2);
var subtree = temp;
Note regarding initial assignment to temp: The meaning of assignment is evaluate the left, evaluate the right, do the assignment. Evaluating the right produces side effects, and those effects must be ordered before the effect of the assignment. See comments for a full discussion.
subtree is not assigned until after the Add calls. This is actually a shorthand for var temp = new ... ; temp.Add(1); temp.Add(2); var subtree = temp;Adds throws, and the assignment is to a field. Do you expect the assignment to have succeeded and visible to code that can see the field even if one of the adds throws? Why should that field be assigned to the collection without a successful Add? That seems very bizarre and error prone.Y y = new Y() { M(y) }; where the variable is a local. Under your proposal this should work, and should pass the new Y() to M before calling Add. That seems bad. If the expression produces a temporary value then y is not assigned at the moment of the call to Add and therefore this code is a compile-time error, as it should be.It's a collection initializer.
Collection initializers let you specify one or more element initializers when you initialize a collection class that implements IEnumerable or a class with an Add extension method. The element initializers can be a simple value, an expression or an object initializer. By using a collection initializer you do not have to specify multiple calls to the Add method of the class in your source code; the compiler adds the calls.