vb.net中的对象生命周期

时间:2016-10-12 12:06:14

标签: vb.net

我对vb.net对象的生命周期有疑问 有两个功能的区别是什么:

功能1:


legend: {
    display: false
}

功能2:

Private Function MyF() As Integer
    Using c As New cMyclass
        If c.somework() = 1 Then Return 1
    End Using
    Return 0
End Function
  • 什么是更好的解决方案?

1 个答案:

答案 0 :(得分:1)

documentation为您提供了非常好的解释。即使有例子。

Using resource As New resourceType 
    ' Insert code to work with resource.
End Using

与此相同

' For the acquisition and disposal of resource, the following
' Try construction is equivalent to the Using block.
Dim resource As New resourceType
Try 
    ' Insert code to work with resource.
Finally 
    If resource IsNot Nothing Then
        resource.Dispose() 
    End If
End Try 

在可能的情况下,始终需要处理对象。