代码之家  ›  专栏  ›  技术社区  ›  d7samurai

Net:使用函数本身来保存返回值而不是声明一个局部变量有什么好处吗?

  •  2
  • d7samurai  · 技术社区  · 15 年前

    最佳实践是什么(在VB.Net中):

    Function GetSomething() as String
        GetSomething = "Here's your string"
    End Function
    

    或

    Function GetSomething() as String
        Dim returnString as String = "Here's your string"
        Return returnString
    End Function
    

    GetSomething 存储返回值而不是声明 returnString 本地然后返回(它是否避免分配/实例化额外的字符串-如果是,是否有任何性能/内存优势)?

    6 回复  |  直到 14 年前
        1
  •  7
  •   steinar Justin CI    13 年前

    有趣的问题。我查过这个:

    Function GetSomething1() As String
        GetSomething1 = "Here's your string"
    End Function
    
    Function GetSomething2() As String
        Dim returnString As String = "Here's your string"
        Return returnString
    End Function
    

    通过IL DASM,结果如下:

    .method public instance string  GetSomething1() cil managed
    {
      // Code size       9 (0x9)
      .maxstack  1
      .locals init ([0] string GetSomething1)
      IL_0000:  nop
      IL_0001:  ldstr      "Here's your string"
      IL_0006:  stloc.0
      IL_0007:  ldloc.0
      IL_0008:  ret
    } // end of method Form1::GetSomething1
    
    .method public instance string  GetSomething2() cil managed
    {
      // Code size       13 (0xd)
      .maxstack  1
      .locals init ([0] string GetSomething2,
               [1] string returnString)
      IL_0000:  nop
      IL_0001:  ldstr      "Here's your string"
      IL_0006:  stloc.1
      IL_0007:  ldloc.1
      IL_0008:  stloc.0
      IL_0009:  br.s       IL_000b
      IL_000b:  ldloc.0
      IL_000c:  ret
    } // end of method Form1::GetSomething2
    

    发布版本:

    .method public instance string  GetSomething1() cil managed
    {
      // Code size       8 (0x8)
      .maxstack  1
      .locals init ([0] string GetSomething1)
      IL_0000:  ldstr      "Here's your string"
      IL_0005:  stloc.0
      IL_0006:  ldloc.0
      IL_0007:  ret
    } // end of method Form1::GetSomething1
    
    .method public instance string  GetSomething2() cil managed
    {
      // Code size       8 (0x8)
      .maxstack  1
      .locals init ([0] string GetSomething2,
               [1] string returnString)
      IL_0000:  ldstr      "Here's your string"
      IL_0005:  stloc.1
      IL_0006:  ldloc.1
      IL_0007:  ret
    } // end of method Form1::GetSomething2
    

    您将注意到调试生成中有更多的操作,但发布生成中没有。

        2
  •  1
  •   David    15 年前

    试两种方法,然后使用 .Net Reflector 看看生成的IL是否不同。

        3
  •  1
  •   Konrad Rudolph    15 年前

    我更喜欢有明确的出口,所以我总是使用 Return . 此外,我不喜欢VBs决定使用函数名作为返回变量的名称(为什么不 Result

    Function Foo() As Integer()
        Foo = New Integer(2) { }
        Dim a = Foo(1) ''// Recursive call or access to return value element?
    End Function
    

    当然,这是一个递归调用(否则这些将不再可能)。但是我对这种不一致性很恼火,所以我不使用语法。

    另一方面,标准化名称对于收益值持有者来说是一个优势,这绝对是比 方法。

        4
  •  0
  •   DarinH    15 年前

    您可以使用reflector来检查,但是,老实说,对于这样的东西,无论性能有什么损失,都会直接落在“过早优化”的标题下。

    OTOH,{var=value;Return var}语法倾向于在函数末尾提升单个出口点。

        5
  •  0
  •   xpda    15 年前

    如果我没记错的话,return()是作为return语句添加到VB6和VB.Net之间的函数中的,可能是为了使它更像其他语言。这就解释了为什么有两种方法可以做同样的事情。

        6
  •  0
  •   Community Mohan Dere    9 年前

    this answer 在我的评论中 this answer Finally 之后 Return