此代码编译成功,但我认为它应该无法编译。此外,当你运行它时,你会得到一个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" }
};
}
}
这是一个已知的错误?