collection expression

C# square-bracket syntax such as [1, 2, 3] or [] that builds whatever collection type the target declares.

In more detail

The same literal creates an array, a List<T> or a HashSet<T>, and the compiler picks the cheapest way to fill it. Two dots, as in [.. tags], spread another collection's items in.

Examples

List<string> names = ["Kettle", "Lamp", "Mug"];
HashSet<string> unique = [.. tags];

Related terms

Taught in

References