Is there a technical reason C# doesn't have a property setter shorthand similar to object initializer, or is it just a design choice?

Viewed 71

If I have an already-created object (e.g. a control on a form) and I want to initialize a number of properties, I have to do this:

myObj.Details.Color = Colors.Red;
myObj.Details.Name = "My Object";
myObj.Details.Location = Location.Here;

Everyone knows about object initializers, e.g. var details = new MyObjectDetails() { Color = Colors.Red, Name = "My Object", Location = Location.Here };, but is there a technical reason that that ability is not provided for existing objects? E.g.

myObj.Details { Color = Colors.Red, Name = "My Object", Location = Location.Here };

Edit:

Here's my real-world example. Third party control (Infragistics UltraWinGrid):

_clientTimeDetailUltraGrid.DisplayLayout.Override.HeaderAppearance.FontData.Name = gridHeaderCellFont.Name;
_clientTimeDetailUltraGrid.DisplayLayout.Override.HeaderAppearance.FontData.SizeInPoints = gridHeaderCellFont.SizeInPoints;

Imagine how tedious that gets.

0 Answers
Related