The readonly
Children
property of StackLayout
is of type IList<View>
. I got surprised knowing Children
can be assigned with collection initializer as shown in the following code snippet (inspired by an example given by Xamarin.Forms documentation):
public MainPage()
{
StackLayout sl = new StackLayout
{
Children =
{
new Label{Text="Start",HorizontalOptions=LayoutOptions.Start}
}
};
}
Question
Why can it be assigned while it is a readonly property?
Next, I made a counter example as follows. Here the compiler complains that Children
is a readonly property. It is understandable but the previous one is not. Could you tell me why?
public MainPage()
{
IList<Label> labels = new List<Label>
{
new Label{Text="Start",HorizontalOptions=LayoutOptions.Start}
};
StackLayout sl = new StackLayout
{
Children = labels
};
}