C#'
C# 참조자복사
왕왕왕왕
2015. 11. 6. 13:38
class Point
{
public int x;
public int y;
public Point()
{
x = 0;
y = 0;
}
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
class Program
{
static void Main()
{
Point pt1 = new Point();
System.Console.WriteLine("({0}, {1})", pt1.x, pt1.y);
Point pt2 = new Point(5, 5);
System.Console.WriteLine("({0}, {1})", pt2.x, pt2.y);
pt1 = pt2; // 참조 복사
System.Console.WriteLine();
System.Console.WriteLine("({0}, {1})", pt1.x, pt1.y);
System.Console.WriteLine("({0}, {1})", pt2.x, pt2.y);
}
}
pt1 에 pt2를 복사한다는 것이, 객체 값을 복사하는게 아니라 참조를 복사하는 것이다.
pt2가 참조자인데 참조자를 복사하는 것이다.