.NET 8 and C# 12 — Collection Expressions

Henrique Siebert Domareski
3 min readNov 23, 2023

Collection Expressions is a new C# 12 feature which allows you to initialise a collection of elements such as Arrays, Spans and Lists in an easier way by using a simpler syntax. In this article, I present how to use this new feature.

Before going to the feature, let’s recap how we initialise an Array, List and Span in C#:

// Array:
int[] myArray1 = new int[] { 10, 20, 30, 40, 50 };
var myArray2 = new int[] { 10, 20, 30, 40, 50 };

// List:
List<string> myList1 = new List<string> { "apple", "banana", "orange" };
var myList2 = new List<string> { "apple", "banana", "orange" };

// Span:
var array3 = new char[] { 'a', 'b', 'c', 'd', 'e' };
Span<char> mySpan1 = array3.AsSpan();
var mySpan2 = array3.AsSpan();

We can create an Array and List by specifying the type (i.e.: int, List<int>, etc) or using the var keyword, but it’s also necessary to declare new int[] or new List<int>.

For demonstration purposes, I created a console application with .NET 8, and I configured the LangVersion in the .csproj file with the preview value:

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
</PropertyGroup>

</Project>

With Collection Expressions, it’s now possible to create Arrays, Spans and Lists in an easier way, without needing to use the keyword new and the type. For example:

// Array
int[] demoArray = [10, 20, 30, 40, 50];

// List
List<string> demoList = ["apple", "banana", "orange"];

// Span
Span<char> demoSpan = ['a', 'b', 'c', 'd', 'e'];

Note that with Collection Expressions, it is not possible to use var, instead, you need to explicitly use the type, and then you can inform the values of the collection.

When printing the values we get the following result:

Console.WriteLine($"{nameof(demoArray)}: {JsonSerializer.Serialize(demoArray)}");
Console.WriteLine($"{nameof(demoList)}: {JsonSerializer.Serialize(demoList)}");
Console.Write($"{nameof(demoSpan)}: ");
foreach (var item in demoSpan) Console.Write($"{item} ");
Console.WriteLine();

// Output
demoArray: [10,20,30,40,50]
demoList: ["apple","banana","orange"]
demoSpan: a b c d e

It’s also possible to use it with a Jagged 2D Array, with Jagged 2D Array from variables and also use it with spread operator (..). For example:

// Jagged 2D Array with Collection Expressions
int[][] demoJagged2DArray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];

// Jagged 2D Array from variables
int[] row0 = [1, 2, 3];
int[] row1 = [4, 5, 6];
int[] row2 = [7, 8, 9];
int[][] demoJagged2DDArrayFromVariables = [row0, row1, row2];

// Array with spread operator
int[] demoWithSpreadOperator = [..row0, ..row1, ..row2];

When printing the values we get the following result:

Console.WriteLine($"{nameof(demoJagged2DArray)}: {JsonSerializer.Serialize(demoJagged2DArray)}");
Console.WriteLine($"{nameof(demoJagged2DDArrayFromVariables)}: {JsonSerializer.Serialize(demoJagged2DDArrayFromVariables)}");
Console.WriteLine($"{nameof(demoWithSpreadOperator)}: {JsonSerializer.Serialize(demoWithSpreadOperator)}");

// Output:
demoJagged2DArray: [[1,2,3],[4,5,6],[7,8,9]]
demoJagged2DDArrayFromVariables: [[1,2,3],[4,5,6],[7,8,9]]
demoWithSpreadOperator: [1,2,3,4,5,6,7,8,9]

Conclusion

Collection Expressions allow you to easily initialise collections such as Arrays, Spans and Lists in an easier and cleaner way, improving the readability of your code.

This is the link for the project in GitHub: https://github.com/henriquesd/DotNet8Examples

If you like this demo, I kindly ask you to give a ⭐️ in the repository.

Thanks for reading!

--

--