Say I have code like this:
var intList = new List<List<int>> {
new() { 1, 2, 3 }
};
I’d like to use a collection expression here. Resharper (EAP) suggests this:
var intList = new List<List<int>> {
[1, 2, 3]
};
But that doesn’t compile:
error CS1003: Syntax error, '=' expected
error CS1525: Invalid expression term ','
Is this possible? If so, what’s the syntax?
>Solution :
The syntax used for the jagged arrays from the What’s new in C# 12:
// Create a jagged 2D array:
int[][] twoD = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
Seems to work for lists also:
List<List<int>> list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];