从问题中得到启发


What is a non-reachable end point(unreachable endpoint) of a statement?


在问这个问题之前,我已经读过:


C# Puzzle : Reachable goto pointing to an unreachable label
Why does this "finally" execute?


问题中的挑战是


  编写一个程序,该程序具有可到达的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();




    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();


我想知道在单线程程序中,难道只有goto指向不可达的标签了吗?以下代码是否也被认为是可行的答案?

here:
    int d=0, n=1/d;
    goto here;

最佳答案

实际上,finally阻止的goto技巧是获得以不可到达的标签为目标的可到达的goto的唯一方法。

关于c# - 唯一的情况是可到达的goto指向不可到达的标签?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15213116/

10-09 19:02