[英]Collection initializer for List<myCustomClass> not working giving “; expected” compile error. Why?
I am trying to initialize a generic collection List something like this: 我正在尝试初始化通用集合列表,如下所示:
List<MyCustomClass> myCustomClassList = new List<MyCustomClass>() {myCustomClassInstance1, myCustomClassInstance2};
I am getting the compile error " ; expected ". 我收到编译错误“ ;预期 ”。 I don't understand this. 我不明白 Shouldn't I just be able to initialize this collection like this? 我是否应该能够像这样初始化此集合?
What version of .NET are you using? 您正在使用什么版本的.NET? Collection initializers only work in .NET 3.5 and higher 集合初始化器仅在.NET 3.5及更高版本中有效
Collection Initializers are part of the C# 3.0 specification and not the .Net Framework/Libraries. 集合初始化器是C#3.0规范的一部分,而不是.Net Framework / Libraries。 The earliest implementation that uses C# 3.0 is VS2008, and the .Net 3.5 framework. 最早使用C#3.0的实现是VS2008和.Net 3.5框架。 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 如果通过命令行或其他IDE通过脚本进行编译,请确保引用了适当的工具链:C:\\ Windows \\ Microsoft.NET \\ Framework \\ v3.5 \\ csc.exe
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.