0

我正在尝试初始化一个通用集合列表,如下所示:

List<MyCustomClass> myCustomClassList = new List<MyCustomClass>() {myCustomClassInstance1, myCustomClassInstance2};

我收到编译错误“ ;预期”。我不明白这一点。我不应该像这样初始化这个集合吗?

4

2 回答 2

4

您使用的是哪个版本的 .NET?集合初始化器仅适用于 .NET 3.5 及更高版本

于 2011-02-28T21:21:08.493 回答
1

Collection Initializers are part of the C# 3.0 specification and not the .Net Framework/Libraries. The earliest implementation that uses C# 3.0 is VS2008, and the .Net 3.5 framework. You can build against earlier versions of the framework. If you are compiling via script from the command-line, or other IDE, make sure you are referencing the appropriate toolchain: C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe

于 2011-02-28T21:38:37.813 回答