且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

有没有人仍然使用[返回]在C#中,如果是这样,为什么?

更新时间:2022-02-27 04:41:57

有一些(罕见)情况下,转到​​可以真正提高可读性。事实上,你的文件链接到列表中的两个例子:

There are some (rare) cases where goto can actually improve readability. In fact, the documentation you linked to lists two examples:

有一个共同使用goto是将控制转移到在switch语句中的特定的switch-case标签或默认标签。

A common use of goto is to transfer control to a specific switch-case label or the default label in a switch statement.

goto语句也很有走出深度嵌套的循环。

The goto statement is also useful to get out of deeply nested loops.

下面是后者的一个例子:

Here's an example for the latter one:

for (...) {
    for (...) {
        ...
        if (something)
            goto end_of_loop;
    }
}

end_of_loop:

当然,还有其他的解决此问题的方法为好,如重构code到一个函数,用它周围的虚拟块,等(见的这个问题了解详细信息)。作为一个方面说明,Java语言的设计者决定禁止的 GOTO 的完全并推出的标签break 的语句。

Of course, there are other ways around this problem as well, such as refactoring the code into a function, using a dummy block around it, etc. (see this question for details). As a side note, the Java language designers decided to ban goto completely and introduce a labeled break statement instead.