int, char, bool 같은 값 형식에 class처럼 사용자 정의 형식을 두려고 할 때 구조체(struct)를 사용한다.
class Test
{
static void Main(string[] args)
{
Vector v1 = new Vector();
Vector v2;
Vector v3 = new Vector(2, 3);
Console.WriteLine(v1.ToString());
// Console.WriteLine(v2.ToString()); //컴파일 에러 : 할당되지 않음.
Console.WriteLine(v3.ToString());
v2.x = 10;
v2.y = 20;
Console.WriteLine(v2.ToString());
}
}
struct Vector
{
public int x;
public int y;
public Vector(int x, int y)
{
this.x = x; this.y = y;
}
public override string ToString()
{
return "x = " + this.x + ", y = " + this.y;
}
}
x = 0, y = 0
x = 2, y = 3
x = 10, y = 20
// 모두 동일한 표현이다.
static void Main(string[] args)
{
int n1 = new int();
int n2;
n2 = 0;
int n3 = 0;
Console.WriteLine(n1);
Console.WriteLine(n2);
Console.WriteLine(n3);
}
class Test
{
static void Main(string[] args)
{
Vector v1 = new Vector(5,10);
Vector v2 = v1;
v2.x = 1000;
v2.y = 3000;
Console.WriteLine(v1.ToString());
Console.WriteLine(v2.ToString());
Point p1 = new Point(4, 10);
Point p2 = p1;
p2.x = 2000;
p2.y = 60000;
Console.WriteLine(p1.ToString());
Console.WriteLine(p2.ToString());
}
}
struct Vector
{
public int x;
public int y;
public Vector(int x, int y)
{
this.x = x; this.y = y;
}
public override string ToString()
{
return "Vector x = " + this.x + ", y = " + this.y;
}
}
class Point
{
public int x;
public int y;
public Point(int x, int y)
{
this.x=x; this.y=y;
}
public override string ToString()
{
return "Point x = " + this.x + ", y = " + this.y;
}
}
Vector x = 5, y = 10
Vector x = 1000, y = 3000
Point x = 2000, y = 60000
Point x = 2000, y = 60000
struct의 깊은 복사는 매개변수로 넘길 때도 동일하게 적용된다.
class Test
{
static void Main(string[] args)
{
Vector v1 = new Vector(5,10);
VectorFunction(v1);
Console.WriteLine(v1.ToString());
Point p1 = new Point(5,10);
PointFunction(p1);
Console.WriteLine(p1.ToString());
}
static void VectorFunction(Vector vt)
{
vt.x = 100;
vt.y = 100;
}
static void PointFunction(Point p)
{
p.x = 100;
p.y = 100;
}
}
Vector x = 5, y = 10
Point x = 100, y = 100
참조값만 전달하는 것이 성능 상 좋지만, 깊은 복사를 해야 한다면 struct로 구현해야 한다. 혹은 GC의 부담을 줄여주기 위해서 구조체를 선택할 수도 있다.