[c#] goto를 써야 할 때

흔히들 goto는 가급적 쓰지 말라고 한다. 나도 좋아하지는 않는다. 레퍼런쓰는 아래와 같은 경우 완전히 빠져나오고 메떠드를 분리하는 걸 권한다.

When you work with nested loops, consider refactoring separate loops into separate methods. That may lead to a simpler, more readable code without the goto statement.
Jump statements – break, continue, return, and goto

private void Form1_Load(object sender, EventArgs e)
{
    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            // ...

            goto Destination;
        }
    }

Destination:

    // ...

    ;
}
void Method1()
{
    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            // ...

            Method2();

            return;
        }
    }
}

void Method2()
{
    // ...
}

그러나 늘 이렇게 상황이 간단한 건 아니다. 예를 들어 중첩 루프nested loop가 메인 뜨레드에서 분리된 뜨레드에서 돌고 있다면 섣불리 빠져나올 수가 없다. 뜨레드에서 벗어나 버리기 때문이다. 이럴 땐 goto가 요긴하다.