Object initializers not working in List<T>.
Posted
by Jebli
on Stack Overflow
See other posts from Stack Overflow
or by Jebli
Published on 2010-04-19T12:45:52Z
Indexed on
2010/04/19
12:53 UTC
Read the original article
Hit count: 247
List<Car> oUpdateCar = new List<Car>();
oUpdateCar.Add(new Car());
oUpdateCar[0].name = "Color";
oUpdateCar[0].value = "red";
oUpdateCar.Add(new Car());
oUpdateCar[1].name = "Speed";
oUpdateCar[1].value = "200";
The above code is working but i want to initialize it when i create the list as below,
List<Car> oUpdateCar = new List<Car>
{
new Car{
name = "Color";
value = "red";}
new Car{
name = "Speed";
value = "200";}
}
The above code is not working. What am i missing. I am using c# .NET 2.0. Please help.
© Stack Overflow or respective owner