这段代码编译通过了,但我认为它应该无法编译通过。此外,当你运行它时,会出现一个 NullReferenceException
异常。缺少的代码是在 Bar
属性的初始化中添加 "new 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" }
};
}
}
这是一个已知的 bug 吗?