中級 › struct(値型)とclass(参照型)のコピーの違い › パターン1
struct(値型)とclass(参照型)のコピーの違い
structは値型、classは参照型です。
1 _________ Point { ^ 2 public int X; 3 } 4 class Program { 5 static void Main(string[] args) { 6 Point p1 = new Point(); 7 p1.X = 5; 8 Point p2 = p1; 9 p2.X = 10; 10 System.Console.WriteLine(p1.X); 11 } 12 }
出力: 10
空欄を埋めてビルドを通す
structは値型なので、代入すると中身がコピーされます。p2を変更してもp1には影響しません。
次の問題