Skip to content
TopicTracker
From HackerNewsView original
TranslationTranslation

Day 1: C# 15 Collection Expressions Just Got Superpowers – with()

C# 15 introduces enhanced collection expressions with the new `with()` syntax, building on earlier features like spread elements and collection literals. This addition allows developers to create modified copies of collections more concisely and expressively, improving code readability and reducing verbosity when working with immutable or modified collection data.

Background

- C# is Microsoft's general-purpose programming language, widely used for building Windows apps, web services, games (via Unity), and enterprise software. It evolves through annual releases; C# 15 is the upcoming version (expected with .NET 10 in late 2025). - Collection expressions are a C# 12 feature that lets developers create lists, arrays, and other collections using a concise bracket syntax (e.g., `int[] numbers = [1, 2, 3];`). The new `with()` construct extends this by allowing "spreading" of an existing collection's elements into a new one, similar to the spread operator in JavaScript or Python. - This matters because it reduces boilerplate when you need a copy of a collection with modifications — a common operation that previously required explicit loops or LINQ calls. For C# developers, it's a quality-of-life improvement that makes common patterns more readable.

Related stories