VB.NET 函数返回

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

VB.NET Function Return

vb.netfunctionreturn

提问by user50612

In order to return a value from a VB.NET function one can assign a value to the "Functions Name" or use "return value."

为了从 VB.NET 函数返回一个值,可以为“函数名称”分配一个值或使用“返回值”。

I sometimes see these inter-mixed in the same function. Personally, I prefer the return.

我有时会看到这些混合在同一个函数中。就个人而言,我更喜欢退货。

My question is, what is the internal difference, if any, between the two?

我的问题是,两者之间的内部差异是什么(如果有的话)?

采纳答案by StingyHyman

There is probably no difference. IIRC, the compiler generated IL converts them both into Return statements unless there is additional usage of a _returnValue variable.

可能没有区别。IIRC,编译器生成的 IL 将它们都转换为 Return 语句,除非额外使用 _returnValue 变量

The readability of the FunctionName assignment is poor in my opinion, and an example of a bad VB6 habit. I prefer the _returnValue (NOT RETVAL) variable method also.

在我看来,FunctionName 赋值的可读性很差,这是一个坏 VB6 习惯的例子。我也更喜欢 _returnValue (NOT RETVAL) 变量方法。

回答by noeldp

The difference is that they DO DIFFERENT THINGS!

不同的是他们做不同的事情!

'Return value' does 2 things:
1. It sets the function return value at that point 2. It immediately exits the function

“返回值”做两件事:
1. 它在那个点设置函数返回值 2. 它立即退出函数

No further code in the function executes!

函数中没有进一步的代码执行!

'Functionname = value' does 1 thing: 1. It sets the function return value at that point

'Functionname = value' 做了 1 件事: 1. 它在那个点设置函数返回值

Other code in the function continues to execute This enables additional logic to refine or override the function return value

函数中的其他代码继续执行 这使附加逻辑能够细化或覆盖函数返回值

Huge difference folks. Remember it's not all about state, it's also about flow.

天壤之别。请记住,这不仅与状态有关,还与流有关。

回答by TGnat

Let's take a look... Oddly the "functionName =" generates less IL?

让我们来看看......奇怪的是“functionName =”产生更少的IL?

Code:

代码:

Public Function Test() As String
    Test = "Test"
End Function


Public Function Test2() As String
    Return "Test"
End Function

IL:

伊尔:

.method public static string Test() cil managed
{
    .maxstack 1
    .locals init (
        [0] string Test)
    L_0000: nop 
    L_0001: ldstr "Test"
    L_0006: stloc.0 
    L_0007: ldloc.0 
    L_0008: ret 
}

.method public static string Test2() cil managed
{
    .maxstack 1
    .locals init (
        [0] string Test2)
    L_0000: nop 
    L_0001: ldstr "Test"
    L_0006: stloc.0 
    L_0007: br.s L_0009
    L_0009: ldloc.0 
    L_000a: ret 
}

回答by Tom Anderson

Doing the following is only provided for Visual Basic 6.0developers to easily port code over:

执行以下操作仅供Visual Basic 6.0开发人员轻松移植代码:

Public Function MyFunction() As String
    MyFunction = "Hello"
End Function

I would definitely not recommend keeping doing it if your project includes anyone who hasn't worked with Visual Basic 6.0, as this syntax will be confusing.

如果您的项目中包含没有使用过 Visual Basic 6.0 的任何人,我绝对不建议继续这样做,因为这种语法会令人困惑。

回答by Joel Coehoorn

99 times out of 100 I'll use "return value".

100 次中有 99 次我将使用“返回值”。

Every once in a while I'll have a function where the other type not only allows me to save a variable declaration, but do it in a way that actually significantly clarifies the function. Usually this happens when I would want to name the return value the same as the function anyway, and often these are recursive functions; something about that construct lends it to the implicit return variable. However, that scenario is extremely rare. I don't know if I have any functions using implicit return variables at all in my current project.

每隔一段时间我都会有一个函数,其中其他类型不仅允许我保存变量声明,而且以一种实际上显着阐明函数的方式来执行。通常,当我想将返回值命名为与函数相同的名称时,通常会发生这种情况,并且通常这些是递归函数;关于该构造的某些东西将其借给了隐式返回变量。不过,这种情况极为罕见。我不知道在我当前的项目中是否有任何使用隐式返回变量的函数。

回答by Alan K

Having read that the Return Value syntax was the One True .NETWay Of Doing Things I thought "OK, we'll do it that way then". Then I wrote a function which I knew, hand on heart KNEW, returned either a value from a Return statement or alternatively an Exception under all circumstances, and still got a compiler warning that the function "doesn't return a value on all paths".

读到返回值语法是一种真正的.NET做事方式后,我想“好吧,我们会那样做”。然后我写了一个我知道的函数,心里知道,在所有情况下从 Return 语句返回一个值,或者返回一个 Exception,并且仍然收到编译器警告,该函数“不会在所有路径上返回值” .

Thankfully I came across the Stack Overflow question How can I make this function not generate a “doesn't return a value on all paths” warning?which explained why; adding a default value assignment to the procedure name at the head of the function prevented the warning in my case as well.

谢天谢地,我遇到了堆栈溢出问题如何使此函数不生成“不返回所有路径上的值”警告?这解释了原因;在函数的开头为过程名称添加默认值分配也防止了我的情况下的警告。

Consequently, even though I'll continue to use the Return Value syntax simply for the sake of syntax consistency, I'll also be assigning a default value to the function name just to prevent the possibility of cluttering up the compile process with bogus warnings.

因此,即使我将继续使用返回值语法只是为了语法一致性,我也会为函数名称分配一个默认值,只是为了防止编译过程因虚假警告而混乱的可能性。

回答by tsolina

its quite handy when working with 3rd party factories(_hsf), you can avoid declaring return variables

它在使用 3rd 方工厂(_hsf)时非常方便,您可以避免声明返回变量

Public Function CreateExtremum(iShape As INFITF.Reference, iDir1 As HybridShapeTypeLib.HybridShapeDirection, iSide1 As Integer, iDir2 As HybridShapeTypeLib.HybridShapeDirection, iSide2 As Integer, iDir3 As HybridShapeTypeLib.HybridShapeDirection, iSide3 As Integer) As HybridShapeTypeLib.HybridShapeExtremum
    CreateExtremum = _hsf.AddNewExtremum(iShape, iDir1, iSide1)
    CreateExtremum.Direction2 = iDir2
    CreateExtremum.ExtremumType2 = iSide2
    CreateExtremum.Direction3 = iDir3
    CreateExtremum.ExtremumType3 = iSide3
    CreateExtremum.Compute()
End Function

回答by Paul Margus

When Tools/Options/Text Editor/All Languages/Code Lens is activated, the Reference Count shows above each Sub, Function, or Property statement.

当工具/选项/文本编辑器/所有语言/代码镜头被激活时,引用计数显示在每个子、函数或属性语句上方。

"Return Value" seems better than "assigning a value to the Functions Name". In the latter case, "Code Lens" produces an inflated Reference Count.

“返回值”似乎比“为函数名称赋值”更好。在后一种情况下,“代码镜头”会产生一个膨胀的引用计数。

' Code Lens reports "0 references" here for Sub Rosa().
Public Sub Rosa()
    Diagnostics.Debug.WriteLine(Test())
    Diagnostics.Debug.WriteLine(Test2())
End Sub

' Code Lens reports "2 references" here for Function Test().
Public Function Test() As String
    Test = "Test"       ' Code Lens counts this as a reference.
End Function

' Code Lens reports "1 reference" here for Function Test2().
Public Function Test2() As String
    Dim strTest2 as String = "Test"
    Return strTest2     ' Code Lens does NOT count this as a reference.
End Function