Lädt...


🔧 Improve Readability of Your Code with C# Collection Expressions


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

C# 12 introduced one interesting feature called collection expressions.
Collection expressions allow writing more concise and readable code when working with collections.

In today's blog post, you will learn about collection expression initializers, various expression usages and the spread element.

On my website: antondevtips.com I share .NET and Architecture best practices.
Subscribe to become a better developer.

Collection Initializers

Collection expressions provide a simple and consistent syntax across many different collection types.
When initializing a collection with a collection expression, the compiler generates code that is functionally equivalent to using a collection initializer.

Let's explore the usual ways to initialize a list:

// Using var
var list1 = new List<int> { 1, 2, 3, 4, 5 };

// Or using new()
List<int> list2 = new() { 1, 2, 3, 4, 5 };

Collection expressions provide a new syntax to initialize collections:

List<int> list = [ 1, 2, 3, 4, 5 ];

You can also replace the usual array initialization:

var array1 = new int[5] { 1, 2, 3, 4, 5 };

var array2 = new int[] { 1, 2, 3, 4, 5 };

var array3 = new[] { 1, 2, 3, 4, 5 };

With a new syntax:

int[] array = [ 1, 2, 3, 4, 5 ];

When using a new syntax, you need to specify a concrete type of collection, as it can't be inferred directly from the collection initialization.

You can use this new collection expression initialization for collections of any type.
Let's explore a few examples:

char[] letters = [ 'a', 'b', 'c', 'd' ];

List<string> names = [ "Anton", "Bill", "John" ];

List<Person> persons = [
    new("Anton", 30),
    new("Bill", 25),
    new("John", 20)
];

Personally, I like this new syntax, it makes code more concise and more readable for me.

Initializing Empty Collections

You can use a new syntax to initialize empty collections in a new more concise and readable way.

Here is the most common approach to initialize an empty array and list:

var emptyArray = new int[0] {};

var emptyList = new List<int>();

When using this approach, you allocate memory to create an empty collection.
So instead, you can use the following initializers that don't allocate any memory at all:

var emptyArray = Array.Empty<int>();

var emptyList = Enumerable.Empty<int>();

But you can replace all these with a new empty collection expression initializer:

int[] emptyArray = [];

List<int> emptyList = [];

This approach is more concise and readable and moreover, the compiler translates it to using Array.Empty<T> and Enumerable.Empty<T> under the hood, so you don't need to worry about memory.

Using a Spread Element

The spread element (.. two dots) allows you to include the elements of one collection into another collection concisely.
This feature is particularly useful when you want to combine or merge collections.

Let's say you have two arrays of numbers, and you want to combine them into a single array:

int[] oneTwoThree = [1, 2, 3];
int[] fourFiveSix = [4, 5, 6];

int[] allNumbers = [..oneTwoThree, 50, 60, ..fourFiveSix];

Console.WriteLine(string.Join(", ", allNumbers));
Console.WriteLine($"Length: {allNumbers.Length}");

This will output the following result:

1, 2, 3, 50, 60, 4, 5, 6
Length: 8

It doesn't end up with numbers, you can combine any objects you want, for example, strings:

string[] greetings = ["Hello", "Hi"];
string[] farewells = ["Goodbye", "See you"];

string[] allMessages = [..greetings, "How are you?", ..farewells];

Console.WriteLine(string.Join(", ", allMessages));
Console.WriteLine($"Length: {allMessages.Length}");

That outputs:

Hello, Hi, How are you?, Goodbye, See you
Length: 5

And objects:

Person[] groupA =
[
    new Person("John", 20),
    new Person("Jane", 22)
];

Person[] groupB =
[
    new Person("Alice", 25),
    new Person("Bob", 27)
];

Person[] allPeople = [..groupA, new Person("Charlie", 30), ..groupB];

foreach (var person in allPeople)
{
    Console.WriteLine(person);
}

That outputs:

Person { Name = John, Age = 20 }
Person { Name = Jane, Age = 22 }
Person { Name = Charlie, Age = 30 }
Person { Name = Alice, Age = 25 }
Person { Name = Bob, Age = 27 }

This approach is really nice to read and it is easier to write such code than manually creating a list that combines all the values.

This reminds me of a spread operator in JavaScript and TypeScript, but in C# it's not that powerful yet.
And in C# it's not an operator but an element.
Hope it will be further improved in the future and will allow us to combine objects with a spread syntax.

Summary

Collection expressions are a great feature introduced in C# 12.
You can use collection initializers and spread element to write more concise and readable code when working with collections.

In your team, you have to decide whether to adapt this feature or not.
But for me, this feature is a complete win comparing to the old ways of working with collections.

On my website: antondevtips.com I share .NET and Architecture best practices.
Subscribe to become a better developer.

...

🔧 Improve Readability of Your Code with C# Collection Expressions


📈 66.36 Punkte
🔧 Programmierung

🎥 Troubleshoot and improve code readability with Gemini Code Assist


📈 39.56 Punkte
🎥 Video | Youtube

🔧 How ts-pattern can improve your code readability?


📈 39.1 Punkte
🔧 Programmierung

🔧 Improve Code Readability and Efficiency - 11 Tips


📈 35.78 Punkte
🔧 Programmierung

🕵️ angular-expressions up to 1.1.1 expressions.compile injection


📈 34.57 Punkte
🕵️ Sicherheitslücken

🪟 Code-Refaktorisierung mit Collection Expressions in .NET vereinfacht


📈 31.03 Punkte
🪟 Windows Tipps

🔧 How To Train Your Codebase: Enhancing Readability, Reusability and Scalability with Modular Code


📈 29.94 Punkte
🔧 Programmierung

🔧 How to structure your code for readability


📈 29.94 Punkte
🔧 Programmierung

🔧 Unleashing the Power of Collection Expressions in .NET 8


📈 27.26 Punkte
🔧 Programmierung

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


📈 27.26 Punkte
🔧 Programmierung

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


📈 27.26 Punkte
📰 IT Nachrichten

🔧 Improving Code Readability


📈 26.62 Punkte
🔧 Programmierung

🔧 Type Hinting and Annotations in Python: Improving Code Readability and Understanding


📈 26.62 Punkte
🔧 Programmierung

📰 Enhancing Readability of Python Code via Annotations


📈 26.62 Punkte
🔧 AI Nachrichten

🔧 Go Code Roast #2: readability.js port


📈 26.62 Punkte
🔧 Programmierung

🔧 Choosing Your Readability & Maintainability Path


📈 26.16 Punkte
🔧 Programmierung

🐧 Firefox Reader View in your terminal - readability-cli - remove bloat from HTML pages


📈 26.16 Punkte
🐧 Linux Tipps

🔧 How to Customize Slack Fonts Using Stylus for Improved Readability


📈 22.84 Punkte
🔧 Programmierung

🔧 Mastering Typography in UI Design: Enhancing Readability and User Experience


📈 22.84 Punkte
🔧 Programmierung

📰 Google Revamps Google News for Readability, Adds Dedicated 'Fact Check' Block


📈 22.84 Punkte
📰 IT Security Nachrichten

🔧 Refactoring for Readability in Ruby: a Showcase


📈 22.84 Punkte
🔧 Programmierung

🔧 Trimming JavaScript: ditch verbosity, gain readability


📈 22.84 Punkte
🔧 Programmierung

📰 Humanizing Word Error Rate for ASR Transcript Readability and Accessibility


📈 22.84 Punkte
🔧 AI Nachrichten

🔧 PHP: basic loops vs. built-in helpers vs. readability


📈 22.84 Punkte
🔧 Programmierung

🪟 LAPS now has passphrases, better readability & more


📈 22.84 Punkte
🪟 Windows Tipps

🔧 Optimizing GitLab CI for Readability and Maintainability : From 1K to 600 Lines!


📈 22.84 Punkte
🔧 Programmierung

🔧 Readability Score Analysis


📈 22.84 Punkte
🔧 Programmierung

💾 Readability Englisch


📈 22.84 Punkte
💾 Downloads

🔧 Get to know Flesh Kincaid Readability Score 📃


📈 22.84 Punkte
🔧 Programmierung

🔧 Decoding Readability Score? Meaning, Use-cases & Approaches


📈 22.84 Punkte
🔧 Programmierung

🔧 Readability and Naming Things


📈 22.84 Punkte
🔧 Programmierung

📰 Firefox 73 Arrives With Default Zoom Level and Readability Backplate


📈 22.84 Punkte
📰 IT Security Nachrichten

matomo