Skip to content

[Proposal]: Mixed object and collection initializers #10185

Description

@CyrusNajmabadi

Mixed object and collection initializers

Summary

Relax the existing restriction that a { ... } initializer following new T(...) must contain only member initializers or only element initializers. A single initializer would be allowed to contain any sequence of the two kinds, interleaved freely. Each element keeps its existing per-kind meaning — member_initializers assign to the named field/property/event (or invoke the indexer), and element_initializers invoke an Add method on the object being initialized.

var div = new HtmlDivElement
{
    Width = "100%",
    Height = "100%",
    new HtmlSpanElement { Style = { FontColor = "red"  }, "Span 1" },
    new HtmlSpanElement { Style = { FontColor = "blue" }, "Span 2" },
};

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions