// 数値の変更 static void changeValue() { PosClass pc = new PosClass(); PosStruct ps; //値なので new しなくても使える pc.x = 1; pc.y = 0; ps.x = 1; ps.y = 5; Console.WriteLine("-------- 変更前"); Console.WriteLine(pc); Console.WriteLine(ps); change(pc, ps); Console.WriteLine("-------- 変更後"); Console.WriteLine(pc); // x=3 参照渡しなので変更は反映される Console.WriteLine(ps); // x=1 値渡しなので変更は反映されない }
// nullは許容されるのか static void nullable() { PosClass pc = null; //PosStruct ps = null; // 値型なので null をセットできない旨のコンパイルエラー }
static void change(PosClass pc, PosStruct ps) { pc.x = 3; ps.x = 3; }