这段代码编译成功,但我认为它应该无法编译.此外,当你运行它时,你会得到一个NullReferenceException.丢失的代码是Bar属性初始化中的"新条形图" .
class Bar
{
public string Name { get; set; }
}
class Foo
{
public Bar Bar { get; set; }
}
class Program
{
static void Main(string[] args)
{
var foo = new Foo
{
Bar = { Name = "Hello" }
};
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个已知的错误?