[C#] struct と class

struct はvalue type、class はreference typeである。
structはstackに積まれ、classはheapに割り当てられる。
classの大元のclassは System.Object。
structの大元は System.ValueType。
classは他のclassを親クラスとしてprivate以外のfield,methodを引き継げる。structには親を継承する宣言はできない。
classのfieldは初期化できる。structでは初期化できない。
classやstructを作った時、初期状態を作るconstructorを実行する。
defaultではパラメタのないconstructorが呼ばれる。
パラメタ付きのconstructorを複数使い,
constructor initializerで初期化コードの重複を避けることができる。
以下ではパラメタ1個のconstructorはパラメタ3個のcontructorを呼び出してから、自分のコード(この場合は空)を実行する。

public Date(int year) : this(year, 1, 1)
{
}
public Date(int year, int month, int day)
{
// 3つのfieldを初期化処理
// 例外処理
....
this.year = year;
this.month = month;
this.day = day;
}

constructorの特殊な形として、staticなfieldを初期化するstatic constructorがある。static constructorはinstance constructorよりも先に実行される。