2020年01月01日

C#のプロパティーの簡潔な書き方

C#でプロパティーという書き方がありますが使っていますか?

float width
{
get { return size.width; }
}

みたいな書き方です。

これは次のような感じで省略して書くことができるみたいです。

float width => size.width;

setも使いたい場合は少し長くなりますが、次のような感じです。

float width
{
get => size.width;
set => set.width = value;
}

Visual Studioの表示で教えてもらったのですが、短かくて読み易くなったような気がします。

リンク

Properties - C# Programming Guide | Microsoft Docs
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties
タグ:C#
posted by unity-chan at 16:08 | Script