有没有人仍然使用[返回]在C#中,如果是这样,为什么? [英] Does anyone still use [goto] in C# and if so why?

查看:130
本文介绍了有没有人仍然使用[返回]在C#中,如果是这样,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人仍然使用在C#中,哪些可能的原因有这样做的转到关键字语法。

I was wondering whether anyone still uses the "goto" keyword syntax in C# and what possible reasons there are for doing so.

我倾向于认为,导致读者跳来跳去的code是不好的做法的任何声明,但不知道是否有任何可信的情况下,使用这样的语法?

I tend to view any statements that cause the reader to jump around the code as bad practice but wondered whether there were any credible scenarios for using such a syntax?

转到关键字定义

推荐答案

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

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.

这篇关于有没有人仍然使用[返回]在C#中,如果是这样,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆