Home Ā» New in .NET 8.0 [4]: Collection Expressions in C# 12.0

New in .NET 8.0 [4]: Collection Expressions in C# 12.0

by admin
New in .NET 8.0 [4]: Collection Expressions in C# 12.0

A very nice innovation from C# 12.0 is the simplified syntax for initializing sets. Microsoft originally called this language feature Collection Literals, but now called Collection Expressions.

Advertisement

Dr. Holger Schwichtenberg is Chief Technology Expert at MAXIMAGO, which offers innovation and experience-driven software development, including in highly critical safety-related areas. He is also head of the expert network www.IT-Visions.de, which supports numerous medium-sized and large companies with advice and training in the development and operation of software with 43 renowned experts.

With this new syntax you can greatly unify the previously very heterogeneous initialization forms of object sets in the style of JavaScript, i.e. with the values ā€‹ā€‹in square brackets, separated by commas:

Previous initialization Now also possible int[] aĀ =Ā newĀ int[3] {Ā 1,Ā 2,Ā 3Ā };

int[] aĀ = [1,2,3];Ā Ā Ā Ā Ā 

Span bĀ =Ā stackalloc[] {Ā 1,Ā 2,Ā 3Ā };

Span bĀ = [1,2,3];Ā Ā 

ImmutableArray cĀ =Ā ImmutableArray.Create(1,Ā 2,Ā 3);

ImmutableArray cĀ = [1,2,3];

List dĀ =Ā new()Ā {Ā 1,Ā 2,Ā 3Ā };

List dĀ = [1,2,3]; From IL e = new List() { 1, 2, 3 }; From IL e = [1, 2, 3];

IEnumerable fĀ =Ā newĀ List()Ā {Ā 1,Ā 2,Ā 3Ā };

IEnumerable fĀ = [1,2,3]; However, an object of type ReadOnlyArray is created!

The syntax with square brackets is possible not only during initial initialization, but also during later assignments:

List sites1, sites2 = [ā€œwww.IT-Visions.deā€]sites3;
sites1 = [ā€œwww.dotnetframework.deā€, ā€œwww.dotnet8.deā€,
ā€œdotnet-lexikon.deā€, ā€œwww.dotnet-doktor.deā€];
sites3 = []; // empty list

With the spread operator .. you can integrate sets into other sets as part of the initialization. The spread operator ensures that a flat list is not nested:

// Create an array from the elements of the arrays using spread operator string[] allSitesAsArray =
[.. sites1, .. sites2, ā€œwww.dotnettraining.deā€, .. sites3]; // Create a list from the elements of the arrays using Spread Operator List allSitesAsList =
[.. sites1, .. sites2, ā€œwww.dotnettraining.deā€, .. sites3]; // Expand the list again allSitesAsList = [.. allSitesAsList, ā€œpowershell-schulungen.deā€]; // List: 7 sites are now in the list foreach (var site in allSitesAsList) { Console.WriteLine(site); }

See also  Three Blizzard Classic Games Join Battle.net - Diablo - Gamereactor

A lot is created with these eight sites, because in addition to the seven sites contained in the sites1, sites2, and sites3 variables, another site has been added.

Output of the previous listing

(Bild:Ā Screenshot / Holger Schwichtenberg)

Collection expressions are not currently possible for dictionary objects (as of C# 12.0). With these you can (as before C# 12.0) either use the initialization using nested curly brackets or use the name-value assignment within the curly brackets via [Name] = do value:

// Initialization with Add() SortedDictionary dic0 = new(); dic0.Add(10, ā€œwww.dotnet-doktor.deā€); dic0.Add(21, ā€œwww.dotnetframework.deā€); dic0.Add(42, ā€œwww.dotnet8.deā€); // Initialization with curly braces SortedDictionary dic1 = new() { { 10, ā€œwww.dotnet-doktor.deā€ }, { 21, ā€œwww.dotnetframework.deā€ }, { 42, ā€œwww.dotnet8.deā€ } }; // Initialization with curly and square brackets // (possible before C# 12.0) SortedDictionary dic2 = new()
{
[10] = ā€œwww.dotnet-doktor.deā€,
[21] = ā€œwww.dotnetframework.deā€,
[42] = ā€œwww.dotnet8.deā€
};

(rme)

To home page

You may also like

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy