Collection initialisers

  • Thread starter Thread starter Mike P
  • Start date Start date
M

Mike P

I am working through some LINQ examples on MSDN, and I have come across
some code that doesn't seem to work here :

http://msdn.microsoft.com/en-us/vcsharp/aa336753.aspx

Is this the right way to use collection initialisers or is this way that
I have found on another website correct :

List<Product> productList = new List<Product>
{
new Product
{
ProductID = 1, ProductName = "Chai", Category =
"Beverages", UnitPrice = 18.0000M, UnitsInStock = 39
},
new Product
{
ProductID = 2, ProductName = "Chang", Category =
"Beverages", UnitPrice = 19.0000M, UnitsInStock = 17
},
new Product
{
ProductID = 3, ProductName = "Aniseed Syrup", Category =
"Condiments", UnitPrice = 10.0000M, UnitsInStock = 13
}
 
The MSDN page you cite is wrong.

I would suggest looking at the actual code from the download if it is
still there. The online samples are known to be garbled beyond
recognition in several cases.

So yes, the MSDN code looks borked: there should be a "new" or 1800 in
there... and from the List<product>, I'd guess "new product {...}" each
time, rather than "new {...}" which would be an anon-type. It also
doesn't return the list it creates!

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top