Allow both collection and property initializer #4879
Replies: 7 comments 8 replies
|
Edit: Sorry, I missed the implication of "Currently, a Children property has to be added." |
|
I've been wanting this for a while, too. I think it's visually easy to separate the difference between |
|
I also need this and I'm sure all UI developers out there will agree, too. |
|
I think there's a lot of merit in this. And i'm willing to champion this. |
|
Perhaps after last member setter, we can have an new HTMLDivElement
{
Width="100%",
Height="100%"; // last member setter
new HTMLSpanElement { Style = { FontColor = "red"}, "Span 1" },
new HTMLSpanElement { Style = { FontColor = "blue"}, "Span 2" },
}Unless it's not needed and compiler can deduce this automatically. Come to think of it, if you encounter an identifier and |
|
The problem I have with this, is that we may have a >1 collections in a class, with same type: Let's see at this example: public class User
{
public int Name { get; private set; }
public ICollection<User> Relatives { get; private set; }
public ICollection<User> Friends { get; private set; }
}
new User()
{
Name = "Alexandra",
new User() { Name = "Albert" }, // Now, how we supposed to distinguish who goes to "Relatives" and who goes to "Friends"?
new User() { Name = "John" }
}Yeah, situations like this (when we have 2 collections of same types in class) is not that frequent. However, if we have such case, it seems to me, that this feature will only bring more confusion. Yes, we can enable this feature only if we have only 1 collection with unique type in class, but it stills feels not right to me. |
|
I have opened a proposal and official discussion thread for this:
Please move further conversation to #10186 so it can feed directly into the LDM review of the proposal. Thanks! |
Uh oh!
There was an error while loading. Please reload this page.
See:
https://stackoverflow.com/questions/7647097/possible-to-mix-object-initializer-and-collection-initializer
Why is this not allowed?
I believe this would not be a breaking change.
This could be useful for example in html generation.
Currently, a Children property has to be added.
instead of
All reactions