简体   繁体   English

List的集合初始化器 <myCustomClass> 不给“; 预期”编译错误。 为什么?

[英]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.

相关问题 为什么显然由List &lt;&gt;的集合初始值设定项克隆对象? - Why are objects apparently cloned by collection initializer for List<>?> 带有集合初始化程序的字典列表 - List of Dictionaries with Collection Initializer 为什么集合初始值设定项不与表达式body属性一起使用? - Why collection initializer is not working together with expression body property? 为什么PUN不提供回调? ProtonNetworking无法正常工作 - Why PUN is not giving callbacks? ProtonNetworking is not working as expected 对象初始化程序强制编译错误 - Object Initializer force compile error 列表<t> as 'out' 参数会导致错误。 为什么?</t> - List<T> as 'out' parameter causes an error. Why? 如何转换`字典 <string, Dictionary<string, List<MyCustomClass> &gt;&gt;`至`Dictionary <string, List<MyCustomClass> &gt;` - How to convert `Dictionary<string, Dictionary<string, List<MyCustomClass>>>` to `Dictionary<string, List<MyCustomClass>> ` 为什么在对象初始值设定项中允许没有“new”的集合初始值设定项但不允许在外部? - Why is a collection initializer without `new` allowed inside an object initializer but not outside? 为什么我会得到“最好的重载列表”<delegationinformation> 集合初始值设定项的 add 方法有一些无效参数”</delegationinformation> - Why am I getting “the best overloaded List<DelegationInformation> add method for the collection initializer has some invalid arguments” ConcurrentQueue操作给出OutofMemory错误。 - ConcurrentQueue Operation is giving OutofMemory Error.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM