The syntax is arbitrary, strictly speaking, but, in my opinion, a way to think that YInstance = { Z = 1 } does not have the new keyword, so it does not call any constructors. However, there is a part of the syntax initializer, so it simply applies the initializer to the properties of the existing one! YInstance YInstance exists in your case because you created it in your X constructor. Here's what happens if you havenโt .
Instead of "Set YInstance for this thing," "Set YInstance properties for these things."
= { ... } in this case means applying this initializer to an existing property value.
The indication that c = between the name of the property and the initializer bindings may at first seem uninteresting, but that it is and like any syntax, you will learn to recognize it quickly enough. You can do the same to initialize the elements of already created collections:
public class C { public List<int> Items { get; } = new List<int>(); }
...
One of the C # team members (if you specify his name, he will appear) after he kindly took the time to comment on the answer to the question about the syntax of the list initializer . They kicked him a lot, but that was the best option they encountered. There are many considerations when it comes to adding syntax for a mature language. They lose a lot of sleep in order to make it clear to the programmer, but at the same time it must be unambiguous for the compiler, it cannot break the existing code, etc. "Syntax is arbitrary" does not mean that the C # team makes arbitrary decisions.
I canโt say why Resharper would mind you taking a weapon against a sea of โโbraces. I like your original version.
source share