Syntax is arbitrary, strictly speaking, but in my view the way to think about it is that YInstance = { Z = 1 }
has no new
keyword, so it's not calling any constructors. However, the initializer part of the syntax is present, so it's merely applying the initializer to the properties of a (hopefully!) existing YInstance
. YInstance exists in your case because you created it in your X
constructor. Here's what would happen if you didn't.
Instead of "Set YInstance to this thing", it's "Set the properties of YInstance to those things".
= { ... }
in this case means to apply that initializer to the existing value of a property.
Indicating that with =
between the property name and the initializer braces may not seem ideally intuitive at first, but it is what it is and like any syntax, you learn to recognize it soon enough. You can do the same to initialize the items of collections that have already been created:
public class C {
public List<int> Items { get; } = new List<int>();
}
...
// This can't be assigning to Items, because it's read-only
var c = new C { Items = { 0, 1, 2 } };
One of the C# team members (if you mention his name, he will appear) once kindly took the time to comment on an answer to a question about the list initializer syntax. They kicked it around a lot, but this was the best option they came up with. There are a lot of considerations when it comes to adding syntax to a mature language. They lose a lot of sleep over making it as clear as possible to the programmer, but at the same time it has to be unambiguous to the compiler, it can't break existing code, etc. "Syntax is arbitrary" doesn't mean that the C# team makes arbitrary decisions.
I can't say why Resharper would object to your taking arms against a sea of curly braces. I like your original version better.