语法:VB.NET 子例程中的“Exit Sub”或“Return”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1004657/
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
Syntax: "Exit Sub" or "Return" in VB.NET subroutines
提问by Jeff
Both "Exit Sub" or "Return" seem to accomplish the same thing -- exit a subroutine. Is there any difference in how they work under the covers?
“退出子程序”或“返回”似乎都完成了同样的事情——退出子程序。他们在幕后工作的方式有什么不同吗?
That is,
那是,
Private Sub exitNow()
Exit Sub
End Sub
or
或者
Private Sub exitNow()
Return
End Sub
回答by RBarryYoung
From the doc:
从文档:
In a Sub or Set procedure, the Return statement is equivalent to an Exit Sub or Exit Property statement, and expression must not be supplied.
在 Sub 或 Set 过程中,Return 语句等效于 Exit Sub 或 Exit Property 语句,并且不得提供表达式。
So they're the same in this context.
所以在这种情况下它们是相同的。
回答by Mike
I tend to prefer Return
over Exit Sub
. Because once in a while you change from Sub
to Function
. In this case Exit Sub
could be converted to Exit Function
, but this assumes that there was a previous assignment to the function name (alike VB 6), which most probably didn't happen. Return
would catch this situation - if the method should return a value, Return
with no argument will fail at compile time.
我倾向于选择Return
了Exit Sub
。因为偶尔您会从Sub
变为Function
。在这种情况下Exit Sub
可以转换为Exit Function
,但这假设先前已分配给函数名称(类似于 VB 6),这很可能没有发生。Return
会捕捉到这种情况 - 如果该方法应该返回一个值,Return
则在编译时没有参数将失败。
回答by dipan chikani
If you inspect the IL output of the 2 statements, they are the same. However, since 'return' is meant for pushing something back to the caller, so strictly speaking, ‘Exit Sub' is more suitable for using in a Sub.
如果您检查 2 个语句的 IL 输出,它们是相同的。但是,由于'return' 的意思是将某些东西推回给调用者,所以严格来说,'Exit Sub' 更适合在 Sub 中使用。
回答by Thiago Burgos
They are the same in this context.
在这种情况下,它们是相同的。
However, from the code readability point of view, "Exit Sub" would be clearer since the "Return" indicates that something some value is being used as an output (which is not the case with Sub-routines).
但是,从代码可读性的角度来看,“退出子”会更清楚,因为“返回”表示某些值正在用作输出(子例程不是这种情况)。
回答by MAMPRO
- First of all, Procedures comes with sub, we should know that we are working on specific procedures that don't return a specific value with the ability of passing some specific parameters or even without passing any parameter. Such as:
- Print something().
- Calculate the factorial of integer number
CalcFact(X)
. Do some processes for a specific task.
Function is a specific process programmed to achieve a specific task by also passing some specific parameters, and it has to return some value that can be used to to complete the overall task, such as validation the user name and user pass.
- 首先,Procedures 自带sub,我们应该知道我们正在处理不返回特定值的特定过程,具有传递某些特定参数甚至不传递任何参数的能力。如:
- 打印东西()。
- 计算整数的阶乘
CalcFact(X)
。 为特定的任务做一些处理。
函数是一个特定的过程,通过传递一些特定的参数来实现特定的任务,它必须返回一些可以用来完成整个任务的值,例如验证用户名和用户通行证。
In short Sub Doesn't return value and we call it directly "Print HelloWorld()"
, whereas functions do such as:
简而言之, Sub 不返回值,我们直接调用它"Print HelloWorld()"
,而函数则例如:
ValidUsersNameAndPass("Johne","jOhNe13042019")
' This could return a Boolean value.ValidUsersNameAndPass("Johne","jOhNe13042019");
// This could return a Boolean value.
ValidUsersNameAndPass("Johne","jOhNe13042019")
' 这可以返回一个布尔值。ValidUsersNameAndPass("Johne","jOhNe13042019");
// 这可以返回一个布尔值。
回答by Slai
I wanted to confirm that they act the same in lambda expressions too, and they do:
我想确认它们在 lambda 表达式中的行为也相同,并且它们确实:
Sub test()
Dim a As Action = Sub() Exit Sub
Dim b As Action = Sub() Return
a()
b()
MsgBox("Yes they do!")
End Sub
回答by Joel Coehoorn
While there are exceptions like guard clauses, in most cases I would consider either a sign that the method is too long.
虽然有像保护子句这样的例外,但在大多数情况下,我会认为这表明该方法太长了。