编译器错误 CS1920
更新:2007 年 11 月
错误消息
元素初始值设定项不能为空。
集合初始值设定项由一系列元素初始值设定项组成。除非元素初始值设定项包含赋值表达式,否则它们不需要用大括号括起。但是,如果确实提供了大括号,则大括号不能为空。如果元素初始值设定项为对象初始值设定项,则只要该初始值设定项包含新的对象创建表达式,大括号就可为空。
更正此错误
在大括号中间添加缺少的表达式。
如果表达式要用作对象初始值设定项,则在大括号的前面添加新的对象创建表达式。
示例
下面的示例生成 CS1920:
// cs1920.cs
using System.Collections.Generic;
public class Test
{
public static int Main()
{
// Error. Empty initializer
// for inner list.
List<List<int>> collection =
new List<List<int>>() { { } }; // CS1920
// OK. No initializer for inner list.
List<List<int>> collection2 =
new List<List<int>>() { };
// OK. Inner list is initialized
// to one List<int> with zero elements.
List<List<int>> collection3 =
new List<List<int>>() { new List<int> { } };
return 0;
}
}