If you have the class:
class Foo {
Bar Bar { get; } = new Bar();
}
class Bar {
string Prop {get; set; }
}
You can use a object initialise like:
var foo = new Foo {
Bar = { Prop = "Hello World!" }
}
If you have a class
class Foo2 {
ICollection<Bar> Bars { get; } = new List<Bar>();
}
You can write
var foo = new Foo2 {
Bars = {
new Bar { Prop = "Hello" },
new Bar { Prop = "World" }
}
}
but, I would like to write something like
var items = new [] {"Hello", "World"};
var foo = new Foo2 {
Bars = { items.Select(s => new Bar { Prop = s }) }
}
However, the code above does not compile with:
cannot assigne IEnumerable to Bar
I cannot write:
var foo = new Foo2 {
Bars = items.Select(s => new Bar { Prop = s })
}
Property Bars is readonly.
Can this be archived?