C# Collection Expressions for Array Initialization
Learn how C# 12 collection expressions simplify array initialization, including target typing, spread elements, and common limitations.

C# 12 introduced collection expressions, a unified syntax for initializing arrays, spans, and other collection types. For array initialization, this replaces the traditional new int[] { ... } with a simpler [ ... ] form. This article explains how collection expressions work for array initialization, where they fit in your code, and what to watch out for.
What Are Collection Expressions?
Collection expressions provide a consistent syntax for creating collections. Instead of writing new int[] { 1, 2, 3 }, you write [1, 2, 3]. The compiler uses the target type to determine the collection type. This works not only for arrays but also for Span<T>, ReadOnlySpan<T>, List<T>, and types that have a suitable collection initializer or collection builder pattern.
For arrays, the syntax is straightforward, but the behavior differs from the old syntax in a few important ways. The most notable difference is that collection expressions require a target type. You cannot use var directly with them because there is no type information on the right-hand side.
Basic Array Initialization
The simplest use case is initializing an array with known elements:
int[] numbers = [1, 2, 3, 4, 5];This is equivalent to the older form:
int[] numbers = new int[] { 1, 2, 3, 4, 5 };The compiler infers the array element type from the target variable. You can also use collection expressions in method calls where the parameter type is known:
PrintNumbers([1, 2, 3]);
void PrintNumbers(int[] numbers)
{
// ...
}This reduces noise and keeps the call site readable. However, you cannot write var numbers = [1, 2, 3]; because the compiler has no target type to infer from. This is a deliberate design decision to maintain type safety.
Using Spread Elements
One of the most useful features of collection expressions is the spread operator ... It expands an existing collection into a new array. For example:
int[] first = [1, 2, 3];
int[] second = [4, 5, 6];
int[] combined = [.. first, .. second]; // [1, 2, 3, 4, 5, 6]The spread operator works with any enumerable type, including arrays, lists, and even IEnumerable<T>. This is a concise way to concatenate collections without calling Concat or manually copying elements. The compiler generates code that allocates a new array and copies each element from the spread sources.
You can mix literal elements and spread elements:
int[] result = [0, .. first, 10, .. second];This produces [0, 1, 2, 3, 10, 4, 5, 6]. The order is preserved exactly as written.
Type Inference and Target Typing
Collection expressions rely on target typing. The compiler determines the element type from the context. For example:
string[] names = ["Alice", "Bob"];If the target type is a Span<T>, the collection expression creates a span over a temporary array. This is efficient for short-lived operations:
Span<int> span = [1, 2, 3];The compiler may allocate a temporary array to back the span. This behavior is important to understand for performance-sensitive code. If you need a span that does not allocate, consider using stackalloc or a pre-allocated buffer instead.
Target typing also affects how the compiler handles the expression. If the target type is int[], the element type is int. If the target type is an interface such as IEnumerable<int>, the compiler still has to choose a concrete backing collection that satisfies the interface; an array is enough for IEnumerable<int>. Do not rely on the exact backing representation without testing the specific target type and compiler version.
When to Prefer Collection Expressions
Collection expressions are preferable when you want concise, readable code for array initialization. They are especially useful in method arguments, where the target type is known. For example:
ProcessData([1, 2, 3]);
void ProcessData(int[] data)
{
// ...
}This avoids the verbose new int[] syntax and makes the intent clearer. Collection expressions also work well with ReadOnlySpan<T> parameters, which are common in performance-oriented APIs.
However, there are cases where the traditional syntax is still necessary. For instance, if you need to create an array with a specific size but no initial elements, you still use new int[10]. Collection expressions do not support a size specifier. Similarly, for multi-dimensional rectangular arrays, you must use the traditional new int[2,3] { ... } syntax. Collection expressions only support single-dimensional arrays and jagged arrays (arrays of arrays).
Compatibility and Language Version
Collection expressions are available in C# 12 and later. If you compile with an older language version, you cannot use this syntax. Basic array construction does not require a .NET 8 runtime, because the compiler lowers the expression to ordinary array-creation code. However, some collection target types, especially Span<T> and types that rely on collection builder attributes, may behave differently depending on the target framework. Always check your project's language version setting in the .csproj file:
<PropertyGroup>
<LangVersion>12</LangVersion>
</PropertyGroup>If you are working in a codebase that must support older language versions, you cannot use collection expressions. In that case, stick with the traditional syntax or use helper methods.
Performance and Allocation Behavior
For simple fixed-size array literals, collection expressions are lowered to code with the same practical behavior as an explicit array initializer. For spread elements, the compiler copies each source element into the new collection; the allocation and copy costs are similar to writing that loop by hand. If you care about performance, profile the concrete target type and data size rather than assuming a single rule.
When using Span<T>, the compiler may allocate a temporary array, which could be a concern in hot paths. In such cases, consider using stackalloc or a pre-allocated buffer if you need to avoid heap allocations.
Collection expressions may also allocate when the target type is an interface or a less direct collection type. For example, assigning to IEnumerable<int> needs a concrete backing collection; an array satisfies that interface, so no extra wrapper is required, but the exact representation is an implementation detail. If allocation behavior matters, profile the specific scenario rather than relying on assumptions about the compiler's output.
Common Mistakes and Edge Cases
One common mistake is assuming that var works with collection expressions. It does not. You must have a target type. Another mistake is using the spread operator with a collection that is not enumerable; the compiler will raise an error. Also, be aware that collection expressions cannot be used to create multi-dimensional arrays directly. For jagged arrays, you can nest collection expressions:
int[][] matrix = [[1, 2], [3, 4]];But for rectangular arrays, you still need the traditional new int[2,2] { ... } syntax.
Another edge case is the interaction with nullable reference types. A string?[] target accepts both null and non-null string elements, because string values are implicitly convertible to string?. A collection expression that contains null cannot be assigned to a string[] target without a nullability warning or error, depending on your nullable context.
Finally, remember that collection expressions are not just for arrays. They work with List<T>, HashSet<T>, and custom collection types that support a collection builder pattern. The same syntax and rules apply, but the allocation behavior may differ. Always test the specific type you are using.
Try matching browser tools
All toolsSmall utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related C# guides
All C# guidesC# Dispose vs Destructor: Key Differences Explained
c# dispose vs destructor: Understand the difference between Dispose and destructors in C#, when each runs, and how to manage unmanaged resources correctly.
C# Variable Declaration: Syntax and Practical Usage
Learn how to declare variables in C# with explicit types, var, and nullable annotations, and understand how defaults, scope, and readability affect your choices.
C# Primary Constructors for Classes: Syntax and Usage
Learn how to use C# primary constructors for classes: syntax, initialization, dependency injection, and limitations with practical examples.
C# IComparer: Custom Sorting with IComparer<T>
Learn how to implement IComparer<T> in C# to define custom sort order for arrays and lists, handle nulls, and avoid common performance pitfalls.
More in C#
All C# guidesThe guides filed next to this one in C#, in library order.