dotnet / dotnet/csharplang

[Proposal]: Mixed object and collection initializers

Open
#10,185 0 comments 0 reactions 1 assignee Claimed by @CyrusNajmabadi View on GitHub
Proposal champion
Dominant language
C#
Stars
12.7k
Forks
1.1k
Avg merge
11h 1m
Merged PRs (30d)
3

Description

# Mixed object and collection initializers

* Specification: https://github.com/dotnet/csharplang/blob/main/proposals/mixed-object-and-collection-initializers.md
* Discussion: https://github.com/dotnet/csharplang/discussions/10186

## 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_initializer*s assign to the named field/property/event (or invoke the indexer), and *element_initializer*s invoke an `Add` method on the object being initialized.

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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.