c# - 这是唯一指向无法访问标签的可访问 goto 的情况吗?

标签 c# unreachable-code

灵感来自问题

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

题目中的挑战是

write a program which has a reachable goto statement but the corresponding labelled statement is unreachable - 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-blocked goto 技巧实际上是获得针对不可到达标签的可到达 goto 的唯一方法。

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

相关文章:

c# - 当我被迫编写无法访问的代码时,我该怎么办?

c# - 用户注销后使用 [Authorize] 属性发布 Ajax 表单 ASP.NET

c# - 用一个上下文更新一个实体,并在另一个上下文中插入一个新实体?

C# Excel 函数 - 检测到无法访问的代码

unreachable-code - 当程序在 ReleaseFast 上无法访问时会发生什么? (字郎)

groovy - 为什么此代码在 Groovy 中警告无法访问?

c++ - 为什么这个程序显示 "unreachable code"警告?我该如何抑制它?

c# - 如何在 C# 中将 DataTable 转换为通用列表

c# - 如何使用资源中的图像?

c# - 将 unicode 字符串从 C# 发送到 Java