Collection expressions are a new feature in C# 12. Collection expressions are used to initialize collections. The following code sample shows how to use collection expressions with arrays.
int[] numbers1 = { 1, 2, 3, 4, 5 }; // array initializer
int[] numbers2 = [1, 2, 3, 4, 5]; // collection expression
Instead of
ImmutableArray<string> arr = ImmutableArray.Create<string>("one", "two", "three", "four", "five");
We can do this:
ImmutableArray<string> arr = ["one", "two", "three", "four", "five"];
The spread operator is used to add elements of a collection to another collection. The following code sample shows how to use the spread operator with arrays.
Instead of this:
ImmutableList<Account> immutableAccounts = accounts.ToImmutableList();
We can do this:
ImmutableList<Account> immutableAccounts = [.. accounts];
The primary constructor is used to initialize members of a class. The following code sample shows how to use the primary constructor with the ImmutableList<T>
class.
Instead of this:
public class RacerComparer : IComparer<Racer>
{
private CompareType _compareType;
public RacerComparer(CompareType compareType) =>
_compareType = compareType;
We can do this:
public class RacerComparer(CompareType compareType) : IComparer<Racer>
{
private CompareType _compareType = compareType;
Or this (and use the compareType as parameter, access it when needed):
public class RacerComparer(CompareType compareType) : IComparer<Racer>
{
- Using primary constructors
httpContext.Response.Headers.Append
instead ofhttpContext.Response.Headers.Add
(which would throw on duplicates)