Introduction
Collection expressions are one of the most exciting and productivity-focused features introduced in modern C#. They simplify how developers create and initialize collections like arrays, lists, and spans. Instead of writing long and repetitive initialization code, you can now use a clean, short, and readable syntax.
In C# 13 and C# 14, collection expressions continue to improve developer experience by making code more concise, reducing boilerplate, and improving readability. This feature is especially useful in real-world applications where collections are used frequently, such as APIs, data processing, and business logic.
In this article, we will understand how collection expressions work, explore practical examples, and learn when to use them in real projects.
What Are Collection Expressions in C#?
Collection expressions allow you to create and initialize collections using a simplified syntax with square brackets [].
Instead of writing:
var numbers = new List<int> { 1, 2, 3, 4 };
You can now write:
List<int> numbers = [1, 2, 3, 4];
This syntax is shorter, cleaner, and easier to read.
Collection expressions work with multiple collection types such as:
Arrays
List
Span
ReadOnlySpan
Immutable collections (in supported contexts)
Key Benefits of Collection Expressions
Cleaner and More Readable Code
The biggest advantage is readability. Developers can quickly understand what the collection contains without scanning complex initialization logic.
Reduced Boilerplate Code
You no longer need to repeatedly write new List<T>() or similar constructors. This reduces unnecessary code and improves maintainability.
Better Developer Productivity
Writing less code means fewer chances of mistakes. It also speeds up development, especially when dealing with multiple collections.
Consistent Syntax Across Collection Types
The same syntax can be used for arrays, lists, and spans, making the language more consistent.
How Collection Expressions Work Internally
Collection expressions are not just syntax sugar; the compiler translates them into appropriate collection creation logic.
For example:
int[] numbers = [1, 2, 3];
This is internally similar to:
int[] numbers = new int[] { 1, 2, 3 };
For lists:
List<int> numbers = [1, 2, 3];
The compiler converts it into a list initialization with elements added efficiently.
Using Collection Expressions with Different Types
Arrays
int[] values = [10, 20, 30];
This is the simplest and most common use case.
Lists
List<string> names = ["Aman", "Rahul", "Priya"];
This improves readability compared to traditional list initialization.
Span and ReadOnlySpan
Span<int> numbers = [1, 2, 3];
This is useful in high-performance scenarios where memory allocation matters.
Mixing Existing Collections
You can also combine collections using the spread operator (..).
int[] first = [1, 2];
int[] second = [3, 4];
int[] combined = [..first, ..second, 5];
This creates a new collection by merging existing ones.
What’s New or Improved in C# 14
C# 14 improves collection expressions by making them more flexible and usable in more scenarios.
Better Type Inference
The compiler can now better understand the target type, reducing the need for explicit type declarations.
Join the conversation! Your thoughts help the community grow.