Skip to main content

How to Create an Array or a List with a Collection Expression in C#

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Square brackets create a collection in C#: [90, 75, 82] becomes an array, a List or another collection type, depending on the variable you assign it to. [] is an empty collection, and .. copies another collection's elements in.

Syntax​

Syntax
<CollectionType> <name> = [<element>, <element>, ...];
<CollectionType> <name> = [];
<CollectionType> <name> = [.. <otherCollection>, <element>];
PartWhat it does
<CollectionType>The type the brackets become, such as int[], List<string> or HashSet<int>.
[<element>, ...]The elements, separated by commas.
[]An empty collection of that type.
.. <otherCollection>A spread element: puts every element of another collection in at that point.

Microsoft's collection expressions reference: "A collection expression is a terse syntax that you can assign to many different collection types. A collection expression contains a sequence of elements between [ and ] brackets." On the spread element: "Use a spread element .. to inline collection values in a collection expression."

The Example​

The example makes an array of three scores, a list of three fruits and an empty array. It then builds a fourth array from the scores plus one more score, and prints how many items each one holds.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which creates an int array of three scores, a List of three fruits and an empty string array with square brackets, spreads the scores into a new array with 100 added, and prints the sizes and the joined values.

main.cs
int[] scores = [90, 75, 82];
List<string> fruits = ["apple", "banana", "cherry"];
string[] empty = [];
int[] more = [.. scores, 100];

Console.WriteLine(scores.Length);
Console.WriteLine(fruits.Count);
Console.WriteLine(empty.Length);
Console.WriteLine(string.Join(", ", more));

The same brackets build three different things, because each variable's type tells C# what to create: an int[], a List<string> and an empty string[]. [.. scores, 100] copies the three scores into a new array and adds 100 after them.

Step 2: Run It​

Terminal
dotnet run main.cs
Output
3
3
0
90, 75, 82, 100

A zsh terminal where dotnet run main.cs prints 3, 3, 0 and 90, 75, 82, 100.

Three scores, three fruits, an empty array and the combined array with four scores. scores itself still has three: the spread copied its elements without changing it. The command is the same in zsh, bash and PowerShell.

Why var Doesn't Work​

A collection expression has no type of its own; it takes the type of whatever it's assigned to. With var, there's nothing to take it from:

main.cs
var numbers = [1, 2, 3];
Console.WriteLine(numbers.Length);
Output
/Users/you/demo/main.cs(1,15): error CS9176: There is no target type for the collection expression.

The build failed. Fix the build errors and run again.

Name the type instead: int[] numbers = [1, 2, 3];.

Common Mistakes​

MistakeWhat happensDo this instead
Declaring the variable with varerror CS9176: There is no target type for the collection expression.Write the type, such as int[] or List<int>
Using brackets for a const or a parameter's default valueNot allowed: these need a compile-time constant, and a collection expression isn't oneUse a static readonly field for a fixed collection
Expecting [.. a, 1] to change aIt doesn't: the spread copies a's elements into a new collectionAssign the result to a variable and use that

Troubleshooting​

SymptomLikely causeFix
CS9176 on a line with square bracketsvar, or another place with no type to convert toGive the variable an explicit collection type
A build error on one elementThe element's type doesn't match the collection's element typeConvert the element, or change the collection type

FAQ​

Can I spread a List into an array?​

Yes. The reference says the expression in a spread element "must be enumerable by using a foreach statement", so a List<T>, an array or any other collection you can loop over works, as long as its elements convert to the new collection's element type.

Can I still create an array the older way?​

Yes. new int[] { 1, 2, 3 } and collection initializers still work. A collection expression is the shorter way to write the same collection, and the same brackets work for arrays, lists and other collection types.

Conclusion​

Write the type on the left and the elements in square brackets on the right: int[] scores = [90, 75, 82];. [] makes an empty collection, and .. copies another collection in. Don't use var, because the brackets need a type to become.

Additional Resources​