Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested object initializer syntax

Resharper has just suggested the following refactoring to me:

// Constructor initializes InitializedProperty but // the UninitializedSubproperty is uninitialized. var myInstance = new MyClass(); myInstance.InitializedProperty.UninitializedSubproperty = new MyOtherClass();  // becomes  var myInstance = new MyClass     {         InitializedProperty = { UninitializedSubproperty = new MyOtherClass() }     }; 

I've never seen this kind of object initialization before. In particular I don't see how

InitializedProperty = { UninitializedSubproperty = new MyOtherClass() } 

makes any sense - it's not assigning anything to InitializedProperty.

Is this behaviour specified anywhere?

like image 333
Rawling Avatar asked May 28 '13 14:05

Rawling


People also ask

What is object initializer in C#?

Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to invoke a constructor followed by lines of assignment statements.

What is an object initializer?

An object initializer is an expression that describes the initialization of an Object . Objects consist of properties, which are used to describe an object. The values of object properties can either contain primitive data types or other objects.

What is object initialization in C++?

Dynamic initialization of object in C++ Dynamic initialization of object refers to initializing the objects at a run time i.e., the initial value of an object is provided during run time. It can be achieved by using constructors and by passing parameters to the constructors.

How are Initializers executed in C#?

Initializers execute before the base class constructor for your type executes, and they are executed in the order in which the variables are declared in your class. Using initializers is the simplest way to avoid uninitialized variables in your types, but it's not perfect.


1 Answers

This syntax is called Object Initialization. C# specification clearly gives a lot of examples on this subject:

7.6.10.2 Object initializers

An object initializer consists of a sequence of member initializers, enclosed by { and } tokens and separated by commas. Each member initializer must name an accessible field or property of the object being initialized, followed by an equals sign and an expression or an object initializer or collection initializer. It is an error for an object initializer to include more than one member initializer for the same field or property. It is not possible for the object initializer to refer to the newly created object it is initializing.

Examples are:

Rectangle r = new Rectangle             {                 P1 = { X = 0, Y = 1 },                 P2 = { X = 2, Y = 3 }             }; 

Compiles down to:

Rectangle r = new Rectangle(); r.P1.X = 0; r.P1.Y = 1; r.P2.X = 2; r.P2.Y = 3; 

Having:

public class Rectangle {     public Rectangle()     {         P1 = new Point(); //default Point for demo purpose         P2 = new Point(); //default Point for demo purpose     }      public Point P1 { get; set; }     public Point P2 { get; set; } } 

and

public class Point {     public int X { get; set; }     public int Y { get; set; } } 

Also consider reading a great chapter 8.3 Simplified initialization in C# in depth book. Jon Skeet provides another look at advantages of using this kind of syntax for initializing tree-like structures.

like image 109
Ilya Ivanov Avatar answered Oct 04 '22 15:10

Ilya Ivanov