I have the following code creating an instance of Root
using an object-initializer:
var r = new Root { Person = new Person { Age = 20, Name = "Hans" } };
From Is there any benefit of using an Object Initializer? I know if we´d have only the inner object Person
this was translated to something like this:
var p = new Person();
p.Age = 20;
p.Name = 20;
What I wonder is how does this affect the nested objects in my first example? Is Person
completely created and than assigned to the new instance of Root
or is this merely translated to something like this:
var r = new Root();
r.Person = new Person();
r.Person.Age = 20; // this would call the Persons getter
r.Person.Name = "Hans"; // this would call the Persons getter
The reason I ask is that the getter and setter to modify a Person
for a given Root
is quite complex and I´d like to avoid to call its getters for the sake of setting the properties for that Person
.