C# 2.0 以降の新機能を一つづつ確認していきます。
以前に一度行ったのですが、公式ドキュメント再編でリンク切れしているところを見つけてしまったので。今ならもっと簡潔なサンプルが欠けるところもあるだろうし、せっかくなので今もう一度確認して行きます。
オブジェクト初期化子
https://docs.microsoft.com/ja-jp/dotnet/csharp/programming-guide/classes-and-structs/object-and-collection-initializers
オブジェクトの作成時にプロパティに、値を割り当てることができる。匿名型の使用時にも使われる。
class Program { static void Main(string[] args) { // X を 10、Y を 20 で初期化 MyPoint p = new MyPoint() { X = 10, Y = 20 }; Console.WriteLine(string.Format("{0}, {1}", p.X, p.Y)); // 10, 20 // 匿名型一つ目のインスタンスの Source プロパティが 1、W プロパティが 2 で初期化 // 匿名型二つ目のインスタンスの Source プロパティが 2、W プロパティが 4 で初期化 // 匿名型三つ目のインスタンスの Source プロパティが 3、W プロパティが 6 で初期化 var anonymous = new[] { 1, 2, 3 }.Select(x => new { Source = x, W = x * 2 }); foreach(var m in anonymous) { Console.WriteLine(string.Format("{0}, {1}", m.Source, m.W)); // 1, 2 // 2, 4 // 3, 6 } } } class MyPoint { internal int X { get; set; } internal int Y { get; set; } }