为什么要使用退出选择?

发布于 2024-08-15 19:44:53 字数 382 浏览 8 评论 0原文

以下是我收集的有关退出选择的几个问题...

  1. 在 VB.NET 中使用退出选择有什么理由吗?
  2. 其原因与性能有关系吗?
  3. 退出选择是否等于break;

实施例1

Select case Name
case "Mary"
'...
case "John"
'...
case else

end select

实施例2

Select case Name
case "Mary"
'...
exit select

case "John"
'...
exit select

case else

end select

Here are a couple of questions I gathered regarding exit select...

  1. Is there any reason for using exit select in VB.NET?
  2. Does the reason have anything to do with performance?
  3. Is the exit select equal to break;?

Example 1

Select case Name
case "Mary"
'...
case "John"
'...
case else

end select

Example 2

Select case Name
case "Mary"
'...
exit select

case "John"
'...
exit select

case else

end select

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悟红尘 2024-08-22 19:44:53

它与在类似 C 语言的 switch 语句中使用 break 关键字不同。使用switch,如果省略中断控制,它将进入下一个情况。使用 Visual Basic Select,控制不会失败;已经隐含了 break

但是,您可以将其用作保护子句,以避免需要在 if 块中将代码嵌套到另一层。例如:

Select Case SomeEnumVar
    Case SomeEnum.SomeValue1
         If Not SomeCondition Then Exit Select
         'Do something
    Case SomeEnum.SomeValue2
         'Do something else
    Case Else
         'Default case
End Select

这比等效代码好一点:

Select Case SomeEnumVar
    Case SomeEnum.SomeValue1
         If SomeCondition Then
             'Do something
         End If
    Case SomeEnum.SomeValue2
         'Do something else
    Case Else
         'Default case
End Select

与其他因素相比,这两个示例之间的任何性能差异几乎肯定是微不足道的。

另一种用途是,如果您有很多案例,并且其中一个案例被放置,那么匹配意味着您想要停止检查所有其他案例。这种情况已经发生了,因此您可能只有一个空的 case 语句。但您也可以添加“退出选择”,以向维护人员明确表示您希望这种情况不会执行任何其他操作。

It's not the same as using the break keyword with switch statements from C-like languages. With a switch, if you omit the break control it will fall through to the next case. With a Visual Basic Select, control does not fall through; a break is already implied.

However, you can use it as a guard clause, to avoid needing to nest code another level in an if block. For example:

Select Case SomeEnumVar
    Case SomeEnum.SomeValue1
         If Not SomeCondition Then Exit Select
         'Do something
    Case SomeEnum.SomeValue2
         'Do something else
    Case Else
         'Default case
End Select

That's a little nicer than this equivalent code:

Select Case SomeEnumVar
    Case SomeEnum.SomeValue1
         If SomeCondition Then
             'Do something
         End If
    Case SomeEnum.SomeValue2
         'Do something else
    Case Else
         'Default case
End Select

Any performance difference between these two samples is almost certainly insignificant compared to other factors.

One other use is if you have a lot of cases, and one of the cases is placed so that a match means you want to stop checking all the others. This already happens, and so you might just have an empty case statement there. But you might also add an Exit Select to make it clear to maintainers that you expect this case not to do anything else.

失眠症患者 2024-08-22 19:44:53

嗯...就像使用 goto...一旦找到正确的大小写,“退出”该大小写就没有用了,因为在 Visual Basic 中它将退出。
在 C# 中,您需要退出案例(在这种情况下,需要中断)。

关键是你可以在案例范围的中间使用它,比如:

Case 1
   Do something
   Do something
   Evaluate
      exit select
   Else
      Do something

它很丑,但你可以这样做......

Well... It is like using a goto... Once you found the correct case there is no use in "exiting" the case since in Visual Basic it will be going out.
In C# you need to exit the case (in that case, with a break).

The point is that you can use it in the middle of the scope of a case, something like:

Case 1
   Do something
   Do something
   Evaluate
      exit select
   Else
      Do something

It's ugly, but you can do that...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文