为什么要使用退出选择?
以下是我收集的有关退出选择的几个问题...
- 在 VB.NET 中使用退出选择有什么理由吗?
- 其原因与性能有关系吗?
- 退出选择是否等于
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...
- Is there any reason for using exit select in VB.NET?
- Does the reason have anything to do with performance?
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它与在类似 C 语言的
switch
语句中使用break
关键字不同。使用switch
,如果省略中断控制,它将进入下一个情况。使用 Visual BasicSelect
,控制不会失败;已经隐含了break
。但是,您可以将其用作保护子句,以避免需要在
if
块中将代码嵌套到另一层。例如:这比等效代码好一点:
与其他因素相比,这两个示例之间的任何性能差异几乎肯定是微不足道的。
另一种用途是,如果您有很多案例,并且其中一个案例被放置,那么匹配意味着您想要停止检查所有其他案例。这种情况已经发生了,因此您可能只有一个空的 case 语句。但您也可以添加“退出选择”,以向维护人员明确表示您希望这种情况不会执行任何其他操作。
It's not the same as using the
break
keyword withswitch
statements from C-like languages. With aswitch
, if you omit the break control it will fall through to the next case. With a Visual BasicSelect
, control does not fall through; abreak
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:That's a little nicer than this equivalent code:
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.
嗯...就像使用 goto...一旦找到正确的大小写,“退出”该大小写就没有用了,因为在 Visual Basic 中它将退出。
在 C# 中,您需要退出案例(在这种情况下,需要中断)。
关键是你可以在案例范围的中间使用它,比如:
它很丑,但你可以这样做......
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:
It's ugly, but you can do that...