次のC#コードについて疑問に思っています。
struct Structure
{
public Structure(int a, int b)
{
PropertyA = a;
PropertyB = b;
}
public int PropertyA { get; set; }
public int PropertyB { get; set; }
}
「すべてのフィールドが割り当てられるまで、「this」オブジェクトは使用できません」というエラーでコンパイルされていません。類似のクラスの場合、問題なくコンパイルされています。
次のようにリファクタリングすることで機能させることができます。
struct Structure
{
private int _propertyA;
private int _propertyB;
public Structure(int a, int b)
{
_propertyA = a;
_propertyB = b;
}
public int PropertyA
{
get { return _propertyA; }
set { _propertyA = value; }
}
public int PropertyB
{
get { return _propertyB; }
set { _propertyB = value; }
}
}
しかし、C#に自動プロパティを導入することの全体的なポイントは、後のコードを記述しないようにすることでした。これは、自動プロパティが構造体に関連していないことを意味しますか?