C#  

How to Use Collection Expressions in C# 13 and C# 14

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.

var numbers = [1, 2, 3];

The compiler automatically determines the type based on context.

Improved Performance Handling

In many cases, the compiler generates more optimized code, especially when working with spans and arrays.

Wider Framework Compatibility

More collection types and scenarios are supported compared to earlier versions.

When Should You Use Collection Expressions?

When You Want Cleaner Code

If your goal is to make your code easy to read and maintain, collection expressions are a great choice.

When Initializing Small to Medium Collections

They work best when initializing collections with a known set of values.

When Working with Immutable or Temporary Data

Collection expressions are ideal for temporary data structures or read-only scenarios.

When Combining Collections

Using the spread operator makes merging collections simple and elegant.

When Performance Matters (Span Usage)

In performance-critical applications, using collection expressions with Span can help reduce allocations.

When You Should Avoid Collection Expressions

Very Large or Dynamic Data

If your collection is built dynamically using loops or external data sources, traditional approaches may be clearer.

Complex Initialization Logic

If initialization involves conditions, transformations, or multiple steps, using standard code is more readable.

Legacy Codebases

If your project uses an older C# version, this feature may not be available.

Best Practices for Using Collection Expressions

  • Use collection expressions for simple and clear initialization

  • Avoid overusing them in complex scenarios

  • Prefer them in modern .NET applications

  • Combine with var when type is obvious

  • Use spread operator carefully to maintain readability

Real-World Example

public List<string> GetTopTechnologies()
{
    var backend = [".NET", "Java", "Node.js"];
    var frontend = ["React", "Angular"];

    return [..backend, ..frontend, "AI"];
}

This example shows how easily you can build and combine collections.

Difference Between Traditional Initialization and Collection Expressions

FeatureTraditional ApproachCollection Expressions
SyntaxVerboseShort and clean
ReadabilityModerateHigh
BoilerplateMoreLess
PerformanceGoodOptimized in many cases

Summary

Collection expressions in C# 13 and C# 14 are a powerful feature that simplifies how developers create and manage collections. They reduce code complexity, improve readability, and increase productivity. By using a clean and consistent syntax, developers can focus more on logic rather than boilerplate code. However, like any feature, they should be used wisely in appropriate scenarios to maintain code clarity and performance.