Why don't all of these variables get treated the same way?

前端 未结 2 592
我寻月下人不归
我寻月下人不归 2021-02-15 00:26

I was checking that the position of variable declarations in VB.NET don\'t matter, except for scope, (for this question) and I thought I better check what happens when they\'re

相关标签:
2条回答
  • 2021-02-15 01:20

    (This is more a comment, but in need of too much code to keep it as one.)

    Reflector does show what is happening:

    <STAThread> _
    Public Shared Sub Main()
    Dim e$__ As New _Closure$__1
    Try 
        Dim e$__2 As New _Closure$__2
        Dim e$__3 As New _Closure$__3
        e$__3.$VB$Local_i = 1
        Do
            Dim e$__4 As _Closure$__4
            e$__4 = New _Closure$__4(e$__4)
            Try 
                Dim e$__5 As New _Closure$__5
                Do
                    Dim e$__6 As _Closure$__6
                    e$__6 = New _Closure$__6(e$__6)
                    e$__6.$VB$NonLocal_$VB$Closure_ClosureVariable_8_5 = e$__5
                    e$__6.$VB$NonLocal_$VB$Closure_ClosureVariable_6_4 = e$__4
                    e$__6.$VB$NonLocal_$VB$Closure_ClosureVariable_6_6 = e$__3
                    e$__6.$VB$NonLocal_$VB$Closure_ClosureVariable_4_4 = e$__2
                    e$__6.$VB$NonLocal_$VB$Closure_ClosureVariable_2_B = e$__
                    Dim e_ As VB$AnonymousDelegate_0 = New VB$AnonymousDelegate_0(AddressOf e$__6._Lambda$__1)
                    e_.Invoke
                Loop While (0 <> 0)
            End Try
            e$__3.$VB$Local_i += 1
        Loop While (e$__3.$VB$Local_i <= 2)
    End Try
    End Sub
    

    (This is based upon my code which includes a Try outside the For.)

    You can see here the For loop (seen as a Do loop with the $VB$Local_i set before) and the inner Do generate closures that do have the previous instance of the closure passed in, but the Try does not get that treatment.

    Still don't know why? Looks like a bug to me. If I don't get a reasonable "excuse" (:-)) in a day or so I'll put it on Connect. (Can someone confirm .NET 4.5 VB11 performs the same?)

    0 讨论(0)
  • 2021-02-15 01:29

    From MSDN (emphasis mine) :

    [...]what the compiler basically does, when it enters a new scope containing a lifted variable, the compiler will check to see if an instance of closure already exists; if so, the compiler will create a new instance of closure and reset the value of the variable from the previous closure.

    Note that the compiler only does the above check if it detects a loop or a GoTo in the function where the closure is generated.

    Link

    0 讨论(0)
提交回复
热议问题