Collection Expressions in .NET 8

Beginning with .NET 8, you have the option to employ the novel collection expression to generate frequently used collection values. A collection expression comprises a concise syntax, which, upon evaluation, can be assigned to a wide array of collection types.

Explore a wealth of information and more within the pages of my book, titled “Rock Your Code: Coding Standards for Microsoft .NET,” now available on Amazon. In this 8th edition, discover an additional 230 pages of content, offering valuable insights to empower you and your team in crafting high-quality, easily modifiable code.

Ensure you get your copy by visiting the following link: https://bit.ly/CodingStandards8

Key Features of Collection Expressions:

  • Concise Syntax: Easily create collections using square brackets [] and comma-separated elements, such as [1, 2, 3].
  • Spread Operator (..): Seamlessly inline elements from existing collections, for instance, [1, ..otherList, 4].
  • Implicit Conversions: Collection expressions automatically convert to supported types, including arrays (int[], string[], etc.), spans (Span<T>, ReadOnlySpan<T>), lists (List<T>), and other types with collection initializers.

The Benefits of Collection Expressions:

  • Improved Readability: Enjoy a more concise and expressive syntax for creating collections.
  • Reduced Verbosity: Say goodbye to repetitive new keywords and type annotations.
  • Enhanced Code Clarity: Experience improved code maintainability and understanding.

Here’s how you could create and initialize a collection (array) before the introduction of .NET 8:

var days = new[] { "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", 
                             "FRIDAY", "SATURDAY", "SUNDAY" };

for (int index = 0; index < days.Length; index++)
{
    Console.WriteLine(days[index]);
}
string [] days = ["MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", 
                          "FRIDAY", "SATURDAY", "SUNDAY"];

for (int index = 0; index < days.Length; index++)
{
    Console.WriteLine(days[index]);
}

Collection expressions can be employed with the following types:

  • System.Span<T>
  • System.ReadOnlySpan<T>
  • Inline arrays
  • Arrays
  • Any situation involving a create method that uses a parameter type of ReadOnlySpan<T>
  • Any type that provides support for a collection initializer, such as List<T>.

Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

If you liked this article, please buy David a cup of Coffee by going here: https://www.buymeacoffee.com/dotnetdave

© The information in this article is copywritten and cannot be preproduced in any way without express permission from David McCarter.

Leave a comment

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