在 VB.NET 中,“退出子”是必要的还是有帮助的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18470803/
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-17 14:49:27  来源:igfitidea点击:

In VB.NET is "Exit Sub" necessary or helpful?

vb.netexitsubroutine

提问by John

When using Sub's, if I'm not returning a value, is it necessary to use:

使用 Sub 时,如果我没有返回值,是否有必要使用:

Public Sub whatever()
       ...
       Exit Sub
End Sub

instead of just

而不仅仅是

Public Sub whatever()
       ...
End Sub

Do I gain anything (memory, speed, etc.) by using "Exit"?

使用“退出”是否可以获得任何东西(内存、速度等)?

Does the Sub exit anyway when it is done even if I don't use the "Exit" statement?

即使我不使用“退出”语句,Sub 在完成后是否会退出?

Thanks.

谢谢。

回答by JaredPar

In this particular case Exit Subis completely unnecessary. It can be used there but is generally considered bad style. The statement is necessary when you want to prematurely leave the method. For example if you detect a specific condition is met and you don't want to execute the rest of the method

在这种特殊情况下Exit Sub是完全没有必要的。它可以在那里使用,但通常被认为是不好的风格。当您想提前离开该方法时,该语句是必要的。例如,如果您检测到满足特定条件并且不想执行该方法的其余部分

Public Sub Example() 
  If SomeCondition Then 
    Exit Sub
  End If

  ' Do other work
End Sub