I'm observing some strange behavior (tested in VS2013) when using array initializer in the following case:
class A
{
public List<int> Items { get; set; }
}
void Main(string[] args)
{
// (1) it compiles, but failing with NRE in runtime
var a = new A { Items = { 1, 2, 3 } };
// (2) it does not compile, as expected
List<int> b = { 1, 2, 3 };
}
Actually I would expect a compiler error in the case (1), the same that I have in case (2): Can only use array initializer expressions to assign to array types. Try using a new expression instead.
But the case (1) compiles without any problem and expectable fails with NullReferenceException
when running. Can someone explain why compiler allows the case (1)?