public int Width { get; }
public int Height { get; }
で出ます。
C# 5では、「まだ"{ get; }"だけではだめなのかな?」と思いながら、
//これらプロパティがpublicで宣言されているため、
//({ get; }と同じように)クラス外からsetが使えない
//ようにprivateで宣言する。
public int Width { get; private set; } public int Height { get; private set; }
のように修正します。
この修正版でコンパイルすると、今度はソースコードの
// セル情報 private struct Cell
{
public int X { get; set; }
public int Y { get; set; }
public Cell(int x, int y) //解説:Cell構造体のコンストラクターです { this.X = x;
this.Y = y; }
}
の所で、
"Generally, you should declare private or protected accessibility for fields. Data that your type exposes to client code should be provided through methods, properties, and indexers. By using these constructs for indirect access to internal fields, you can guard against invalid input values. A private field that stores the data exposed by a public property is called a backing store or backing field. You can declare public fields, but then you can't prevent code that uses your type from setting that field to an invalid value or otherwise changing an object's data."
とでており、矢張りアクセサーによりアクセスすることが出来るprivate or protectedの「黒子となる変数(記憶領域)やフィールド」という「裏打ち、後ろ盾」を意味するようです。
//セル情報 private struct Cell
{ /* 旧いコンパイラーは自動実装でエラーが出る
public int X {get; set;}
public int Y {get; set;}
public Cell(int x, int y)
{
this.X = x;
this.Y = y;
}
*/ private int _x; //解説:プロパティXの「黒子」変数 private int _y; //解説:プロパティYの「黒子」変数 public int X
{
//解説:get、setを実装する get{return _x;}
set{ _x = value;} }
public int Y
{ //解説:get、setを実装する get{return _y;}
set{ _y = value;} }
public Cell(int x, int y) //解説:Cell構造体のコンストラクター { //解説:Cellインスタンスが生成された段階で記憶領域が割り当てられる。 _x = x; _y = y; }
}