相关疑难解决方法(0)


C#Puzzle:可访问的转到指向无法访问的标签

这是Eric Lippert在这篇文章中的评论:

现在您已经知道了答案,您可以解决这个难题:给我写一个程序,其中有一个可到达的goto到达一个无法访问的标签. - Eric Lippert 7月17日7:17

我无法创建一个代码,该代码具有指向无法访问标签的可访问goto.这甚至可能吗?如果是的话,C#代码会是什么样子?

注意:我们不讨论'goto'是如何不好等等.这是一个理论练习.

c# goto

4
推荐指数
1
解决办法
1100
查看次数

它是唯一一种指向无法访问的标签的可访问goto吗?

灵感来自问题

在我提出这个问题之前,我读过:

问题的挑战是

编写一个程序,该程序具有可访问的goto语句,但相应的标记语句无法访问 - Eric Lippert

一个可行的答案就像

    // the 3 lines are not important but declare variable for afterwards use
    var whateverException=new Exception("whatever exception");
    var whateverAction=default(Action);
    whateverAction=() => whateverAction();
Run Code Online (Sandbox Code Playgroud)
    try {
        goto whateverLabel; // (1) the goto is reachable
    }
    finally {
        throw whateverException; // (3) because finally hijacks
    }

whateverLabel: // (2) but the label is not really reached
    whateverAction();
Run Code Online (Sandbox Code Playgroud)

我想知道在一个单线程程序中,它是唯一一个指向无法访问标签的可达goto吗?以下代码也被认为是可行的答案吗?

here:
    int d=0, n=1/d;
    goto here;
Run Code Online (Sandbox Code Playgroud)

c# unreachable-code

1
推荐指数
1
解决办法
242
查看次数

标签 统计

c# ×3

.net ×1

finally ×1

goto ×1

unreachable-code ×1