stu*_*rtd 3 c# collection-initializer .net-8.0 c#-12.0
假设我有这样的代码(或任何类型的锯齿状结构):
var intListList = new List<List<int>> {
new() { 1, 2, 3 }
};
Run Code Online (Sandbox Code Playgroud)
我想在这里使用集合表达式。Resharper (EAP) 建议这样做:
var intListList = new List<List<int>> {
[1, 2, 3]
};
Run Code Online (Sandbox Code Playgroud)
但这不能编译。
这一定是可能的!语法是什么?
编辑:
好的。因此,从答案中,不是这样,而是在表达式中声明类型:
// does not compile
var intListList = new List<List<int>> {
[1, 2, 3]
};
Run Code Online (Sandbox Code Playgroud)
类型必须显式声明,并且没有new()
:
// compiles!
List<List<int>> intListList = [[1, 2, 3]];
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
614 次 |
最近记录: |