开发者

Collection initializer for List<myCustomClass> not working giving "; expected" compile error. Why?

开发者 https://www.devze.com 2023-02-14 12:17 出处:网络
I am trying to initialize a generic collection List something like this开发者_运维知识库: List<MyCustomClass> myCustomClassList = new List<MyCustomClass>() {myCustomClassInstance1, myCust

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? Collection initializers only work in .NET 3.5 and higher


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号