重複の可能性:
初期化構文
デモ用の短いコード サンプル (VS2010 SP1、64 ビット Win7):
class A
{
public string Name { get; set; }
}
class B
{
public A a { get; set; }
}
// OK
A a = new A { Name = "foo" };
// Using collection initialiser syntax fails as expected:
// "Can only use array initializer expressions to assign
// to array types. Try using a new expression instead."
A a = { Name = "foo" };
// OK
B b = new B { a = new A { Name = "foo" } };
// Compiles, but throws NullReferenceException when run
B b = new B { a = { Name = "foo" } };
最後の行がコンパイルされているのを見て驚き、実行時に爆発する前に気の利いた (一貫性はありませんが) ショートカットを見つけたと思いました。その最後の使用法には有用性がありますか?