vb.net 为什么要使用退出选择?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1924128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:33:48  来源:igfitidea点击:

Why should I use exit select?

vb.netselect-case

提问by OrElse

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;?
  1. 是否有任何理由在 VB.NET 中使用退出选择?
  2. 原因与性能有关系吗?
  3. 退出选择是否等于break;

Example 1

示例 1

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

end select

Example 2

示例 2

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

case "John"
'...
exit select

case else

end select

回答by Joel Coehoorn

It's not the same as using the breakkeyword with switchstatements 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 breakis already implied.

breakswitch在类 C 语言的语句中使用关键字不同。使用 a switch,如果您省略中断控制,它将进入下一个案例。使用 Visual Basic Select,控制不会失败;abreak已经是隐含的。

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

但是,您可以将其用作保护子句,以避免需要在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

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.

另一种用途是如果您有很多案例,并且放置了其中一个案例,以便匹配意味着您想要停止检查所有其他案例。这已经发生了,所以你可能只有一个空的 case 语句。但是你也可以添加一个 Exit Select 来让维护者清楚你希望这个案例不会做任何其他事情。

回答by gbianchi

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).

嗯...就像使用 goto... 一旦你找到了正确的案例,“退出”案例就没有用了,因为在 Visual Basic 中它会消失。在 C# 中,您需要退出案例(在这种情况下,休息一下)。

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...

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