I am using an object initializer for a st
object:
public class Container
{
public Container () { ContainedItem = new Item; }
public Item ContainedItem { get; set; }
}
public class Item
{
public string Value { get; set; }
}
var MyContainer = new Container()
{
// I want to populate the the property Value of the property Item
// with the second line rather than the first
ContainedItem = new Item() { Value = FooString }, // This works
ContainedItem.Value = FooString // This assigns to the same member but does not work
};
The second initializer line gives the error:
The name 'ContainedItem' does not exist in the current context.
Invalid initializer member declarator.
and suggests declaring ContainedItem
somewhere in local scope.
Now as the first line works it can be seen that ContainedItem
is in fact a valid property of Container
and that MyContainer.ContainedItem
is definitely not null... so why does the following line fail to recognise it?