튜플의 반환값을 분해하는 구문을 원한다면 Deconstruct 메서드를 1개 이상 정의하여 구현할 수 있다. 분해가 되는 개별값을 out 매개변수를 이용해 처리하면 된다.
✍ 샘플코드
class Program
{
static void Main(string[] args)
{
Rectangle r= new Rectangle(3, 10, 200, 500);
(int a, int b) = r;
Console.WriteLine(a +" : "+b);
(int v1, int v2, int v3, int v4 ) = r;
Console.WriteLine(v1 +" : "+v2 + " : "+v3 + " : "+v4);
}
}
class Rectangle
{
public int X;
public int Y;
public int Width;
public int Height;
public Rectangle(int x, int y, int width, int height)
{
X = x;
Y = y;
Width = width;
Height = height;
}
public void Deconstruct(out int positionX, out int positionY)
{
positionX = X;
positionY = Y;
}
public void Deconstruct(out int positionX, out int positionY, out int Garo, out int Sero)
{
positionX = X;
positionY = Y;
Garo = Width;
Sero = Height;
}
}
✅ 결과
3 : 10
3 : 10 : 200 : 500