2

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 }?

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

10 Comments

Though this gets the idea across, I note that 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;
@EricLippert: Thank you for chiming in on this. Updated my answer accordingly. Why is a temporary variable used? My naive assumption is that the compiler could enforce the order "Allocate and assign subtree, call subtree.Add(1), call subtree.Add(2)".
A collection initializer simply produces a value; the fact that you personally like to use that value as the initial value of a local variable declaration is a fact about you, not about the collection initializer. That value could be an operand, an argument, or simply discarded; it need not be assigned to anything.
Second, suppose one of the 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.
Third, what should this do? 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.
|
1

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.

1 Comment

Closest answer, and points the OP to what the class Tree should do/have in order to enable this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.