以下代码:
Imports Microsoft.VisualBasic
Imports system
Public Class hello
Private myName As String
Public Sub New(ByVal name As String)
myname = name
console.writeline("Hello,{0}!", name)
End Sub
Protected Overrides Sub finalize()
console.writeline("GoodBye,{0}!", myname)
End Sub
End Class
Public Class application
Public Shared Sub main()
Dim i As Integer
For i = 1 To 5
Dim hello As New hello(i.tostring)
Next i
End Sub
End Class
得到结果:
Hello,1!
Hello,2!
Hello,3!
Hello,4!
Hello,5!
GoodBye,5!
GoodBye,1!
GoodBye,4!
GoodBye,3!
GoodBye,2!
书上解释为:
"看起来对象似乎是按照与创建对象相反的顺序释放的,表明是插入到了一个堆栈是的结构中.事实上这就是所发生的事情."(VisualBasic.Net面向对象编程,J.P.Hamilton)
但是以下例子:
Imports Microsoft.VisualBasic
Imports system
Public Class hello
Private bigchunkomemory(10000) As Byte
Private myName As String
Public Sub New(ByVal name As String)
myname = name
console.writeline("Hello,{0}!", name)
End Sub
Protected Overrides Sub finalize()
console.writeline("GoodBye,{0}!", myname)
End Sub
End Class
Public Class application
Public Shared Sub main()
Dim i As Integer
For i = 1 To 10000
Dim hello As New hello(i.tostring)
Next i
End Sub
End Class
输出的结果中有一段:
Hello,112!
Hello,113!
Hello,114!
Hello,115!
GoodBye,101!
GoodBye,15!
GoodBye,14!
GoodBye,13!
另一段:
GoodBye,3!
GoodBye,2!
GoodBye,1!
GoodBye,100!
GoodBye,99!
GoodBye,98!
GoodBye,97!
怎么会15~1会比100先释放呢?如果如书上说是“堆栈”的话?