Feature Request: Syntax Sugar for AddRange in List Initialization #7790
Unanswered
Dogwei
asked this question in
Language Ideas
Replies: 1 comment
-
Ah. Here is the solution: C# 12 introduces the syntax "Collection Expression" to describe such usages: var list = (List<int>)[1, .. array, 2, .. array, 3];
// Or
List<int> list = [1, .. array, 2, .. array, 3]; Because a collection expression doesn't contain a natural type, we cannot use // Here 'list' may be a 'List<int>'.
var list = [1, .. array, 2, .. array, 3]; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I would like to propose a new syntax for Roslyn. Here is the idea:
Currently, if we want to initialize a list with an array and some other elements, we have to do something like this:
Referring to the syntax of other languages, I propose a new syntax that allows us to do the same thing more concisely:
In this proposed syntax,
...array
would be equivalent toAddRange(array)
. This would make the code more readable and easier to write.Beta Was this translation helpful? Give feedback.
All reactions