C# Feature - Collection expression arguments
Collection expression arguments
Section titled “Collection expression arguments”Collection expression arguments let you pass constructor or factory arguments into a collection expression by using a leading with(...) element.
Introduced In
Section titled “Introduced In”This feature is documented as part of C# 15 preview. The official Microsoft Learn page is dated March 20, 2026 and says C# 15 preview is available with the .NET 11 preview SDK and Visual Studio 2026 Insiders.
Why This Feature Exists
Section titled “Why This Feature Exists”Collection expressions already made collection creation concise. This feature extends that syntax so callers can still pass setup values such as capacity or comparers without falling back to more verbose construction code.
Example
Section titled “Example”string[] values = ["one", "two", "three"];
List<string> names = [with(capacity: values.Length * 2), ..values];HashSet<string> set = [with(StringComparer.OrdinalIgnoreCase), "Hello", "HELLO"];When To Use It
Section titled “When To Use It”- Use it when collection expressions are the clearest representation and the target collection still needs constructor-like arguments.
- Use it to preserve concise syntax without losing important collection configuration.
Common Pitfalls
Section titled “Common Pitfalls”- This feature is preview-only as of March 20, 2026.
- Use it when the target collection is obvious; otherwise the syntax can feel too implicit.