What are Collection Expressions?
In C# 12, a new syntax was introduced using square brackets
[ ] to create and initialize collections. This provides a single, consistent way to work with Arrays, Lists, Spans, and more.csharp
The Spread Operator (..)
Wait! You can also use the Spread Operator to combine multiple collections into a new one.
csharp
Why use them?
- Consistency: You no longer need to remember if you should use
new[],new List<T>, orStackalloc. Just use[]. - Performance: The compiler is smart enough to choose the most efficient way to allocate memory for your specific collection.
- Cleanliness: Removes the "noise" of repeated type names during initialization.
Summary
| Feature | Syntax | Description |
|---|---|---|
| Brackets | [1, 2, 3] | Unified collection initialization |
| Spread | ..collection | Merging collections into one |
| Target | Type Inference | The compiler knows the type from the variable |
| Best For | Arrays, Lists, Spans, Sets, and custom collections | |
| Logic | "One syntax to rule them all" | |
| Safety | Prevents null exceptions during initialization | |
| Modern | Brings C# parity with languages like Python and JavaScript | |
| Efficiency | Optimized by the compiler for each target type | |
| Standard | Use [] for all collection initialization in C# 12+ | |
| Clean | Great for passing collections as arguments to methods |