0

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?

richard
  • 12,263
  • 23
  • 95
  • 151

2 Answers2

4

What version of .NET are you using? Collection initializers only work in .NET 3.5 and higher

LorenVS
  • 12,597
  • 10
  • 47
  • 54
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

Ritch Melton
  • 11,498
  • 4
  • 41
  • 54