为什么不是所有这些变量都得到相同的处理? [英] Why don't all of these variables get treated the same way?

查看:147
本文介绍了为什么不是所有这些变量都得到相同的处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我检查VB.NET中变量声明的位置无关紧要,除了scope,(对于这个问题),我想我更好地检查当他们解除到闭包时会发生什么。我没有阅读规格,但我不能解释这些结果:

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 "lifted" into a closure. I haven't read the spec, but I can't explain these results:

Dim outer As Integer
For i = 1 To 2
 Dim inner As Integer
 Try
  Dim inner2 As Integer
  Do
   Dim inner3 As Integer
   Call Sub()
    Dim inner4 As Integer
    Console.WriteLine(outer & ", " & inner & ", " & inner2 & ", " & inner3 & ", " & inner4)
    outer = i
    inner = i
    inner2 = i
    inner3 = i
    inner4 = i
   End Sub()
  Loop Until True
 Finally
 End Try
Next

上述输出:

0, 0, 0, 0, 0
1, 1, 0, 1, 0

inner4 每次重置都有意义, innerX ,但为什么只有 inner2 ?!

inner4 being reset each time makes sense, as would all or none of the other innerX, but why only inner2?!

推荐答案

(这是一个注释,但需要太多的代码来保持它一样。)

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

>正在发生什么:

<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

(这是基于我的代码,包括之外尝试

这里是 For 循环(作为一个 Do 循环与 $ VB $ Local_i set before)和内部 Do 生成闭包,它们具有先前的闭包实例,但 Try 没有得到治疗。

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.

仍然不知道为什么?看起来像一个bug给我。如果我在一天左右没有得到合理的借口(:-)),我会把它放在Connect上。 (有人可以确认.NET 4.5 VB11执行相同吗?)

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

这篇关于为什么不是所有这些变量都得到相同的处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆