I'm trying to understand/reconcile my understanding of how C# structs operate when chaining using List and Arrays. I follow Eric's explanation in this post Performance of array of struct types
but I'm taking this one step further.
struct Foo { public int A; public int B; public Foo[] ChildFoos; }
Foo parentFoo = new Foo();
parentFoo.ChildFoos = new Foo[1];
parentFoo.ChildFoos[0] = new Foo();
List<Foo> list = new List<Foo>();
list.Add(parentFoo);
But the behavior changes when you chain the calls. list[0] should be returning a value (not a variable) where as ChildFoos[0] should be returning a variable.
list[0].ChildFoos[0].A = 4545;
In this case what appears to be happening is list[0] is being treated as a variable instead of a value. ChildFoos[0].A maintains it's new value in the parentFoo within the List. How is this possible?
Compare this with attempting to set A on the parent Foo struct which won't compile.
list[0].A = 877;
This will not compile