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
- array — A sequence of same-sized slots stored one after another in a single block of memory, so any slot can be reached by its index directly.
- List<T> — The .NET growable array: items keep their order, reading by index is constant time, and searching by value walks the whole list.
- target-typed new — Writing new() without a type name where the type is already known from the left-hand side, as in Dictionary<string, decimal> prices = new();.
Taught in
- The C# Language: Types, Variables, and Flow Control — Modern .NET Core, C# & Enterprise Microservices
- Collections, Generics, and LINQ — Modern .NET Core, C# & Enterprise Microservices
- Records, Pattern Matching, and Modern C# — Modern .NET Core, C# & Enterprise Microservices