ZetCode

C# collection expressions

last modified October 5, 2026

Collection expressions provide a concise way to create arrays and other collection types. Introduced in C# 12, they use square brackets and are target-typed: the surrounding context determines the collection type.

C# collection expression example

Assign a collection expression to an array, list, or span. Unlike an implicitly typed array initializer, a collection expression generally needs a target type, so var values = [1, 2, 3] does not compile.

Program.cs
using System;
using System.Collections.Generic;

int[] numbers = [2, 4, 6];
List<string> names = ["Ada", "Linus"];
ReadOnlySpan<int> view = [1, 3, 5];

Console.WriteLine(string.Join(", ", numbers));
Console.WriteLine(string.Join(", ", names));
Console.WriteLine(view.Length);
$ dotnet run
2, 4, 6
Ada, Linus
3

The destination type provides the target context. The compiler can convert an expression to supported collection types such as arrays, lists, spans, and types that implement the collection-builder pattern.

Empty collection expressions

An empty pair of brackets creates an empty collection of the target type.

Program.cs
int[] values = [];
List<string> names = [];

Console.WriteLine(values.Length);
Console.WriteLine(names.Count);
$ dotnet run
0
0

Spread elements

The spread operator .. includes elements from another collection. A collection expression can combine individual values with one or more spreads.

Program.cs
int[] first = [1, 2];
int[] second = [..first, 3, ..new[] { 4, 5 }];

Console.WriteLine(string.Join(", ", second));
$ dotnet run
1, 2, 3, 4, 5

A spread expression enumerates or copies values into the destination collection according to the target type. It does not make the resulting array a live view of the original collection.

Collection expressions and interfaces

The target type can be an interface such as IEnumerable<T> or IReadOnlyList<T>. The compiler chooses a suitable collection implementation for the expression. Use an explicit concrete type when your code depends on a particular implementation or mutating API.

Source

Collection expressions - C# reference

This article introduced collection expressions in C# 12.

Author

Jan Bodnar writes programming tutorials and educational content.

List all C# tutorials.