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

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

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:09

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

    Reflector does show what is happening:

     _
    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?)

提交回复
热议问题