Architect's Log

I'm a Cloud Architect. I'm highly motivated to reduce toils with driving DevOps.

【C#6.0の新機能】Auto-property initializers

Visual Studio 2015 Previewのマシンイメージを使って、Azure仮想マシンを立てたので、C#6.0の新機能を試してみます。

今回は、Auto-property initializersです。

C#5.0まで

C#5.0までは、初期値付きのプロパティを以下のいずれかで記述する必要がありました。

class Human {
private string name = "Sato Taro";

public string Name {
    get { return name; }
    set { name = value; }
}
class Human {
    public string Name { get; set; }

    public Human() {
        this.Name = "Sato Taro";
    }
}

C#6.0

C#6.0では、以下のように1行で記述できます。

class Human {
    public string Name { get; set; } = "Sato Taro";
}