dotnet / csharplang

The official repo for the design of the C# programming language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature Request: Syntax Sugar for AddRange in List Initialization

Dogwei opened this issue · comments

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:

var array = new int[] { 1, 2, 3 };
var list = new List<int>();
list.Add(1);
list.AddRange(array);
list.Add(2);
list.AddRange(array);
list.Add(3);

Referring to the syntax of other languages, I propose a new syntax that allows us to do the same thing more concisely:

var array = new int[] { 1, 2, 3 };
var list = new List<int>
{
    1,
    ...array,
    2,
    ...array,
    3,
};

In this proposed syntax, ...array would be equivalent to AddRange(array). This would make the code more readable and easier to write.