Display order in which base-class and derived-class constructors and finalizers are called : Finalize « Development « VB.Net Tutorial






Module Tester

   Sub Main()
      Dim circle1, circle2 As Circle

      circle1 = New Circle(72, 29, 4.5) ' instantiate objects
      circle2 = New Circle(5, 5, 10)

      circle1 = Nothing ' mark objects for garbage collection
      circle2 = Nothing

      System.GC.Collect() ' request garbage collector to execute
   End Sub ' Main 

End Module 

Public Class Point
   Private mX, mY As Integer

   Public Sub New()
      Console.WriteLine("Point constructor: {0}", Me)
   End Sub ' New

   Public Sub New(ByVal xValue As Integer, _
      ByVal yValue As Integer)
      Console.WriteLine("Point constructor: {0}", Me)
   End Sub ' New

   Protected Overrides Sub Finalize()
      Console.WriteLine("Point Finalizer: {0}", Me)
      MyBase.Finalize() ' call Object finalizer
   End Sub 

   ' return String representation of Point
   Public Overrides Function ToString() As String
      Return "[" & mX & ", " & mY & "]"
   End Function ' ToString

End Class


Public Class Circle
   Inherits Point 

   Private mRadius As Double

   Public Sub New()
      Console.WriteLine("Circle constructor: {0}", Me)
   End Sub ' New

   Public Sub New(ByVal xValue As Integer, _
      ByVal yValue As Integer, ByVal radiusValue As Double)

      MyBase.New(xValue, yValue)
      Console.WriteLine("Circle constructor: {0}", Me)
   End Sub ' New

   Protected Overrides Sub Finalize()
      Console.WriteLine("Circle Finalizer: {0}", Me)
      MyBase.Finalize() ' call Point finalizer
   End Sub ' Finalize

   Public Overrides Function ToString() As String

      Return "Center = " & MyBase.ToString() & _
         "; Radius = " & mRadius
   End Function ' ToString

End Class
Point constructor: Center = [0, 0]; Radius = 0
Circle constructor: Center = [0, 0]; Radius = 0
Point constructor: Center = [0, 0]; Radius = 0
Circle constructor: Center = [0, 0]; Radius = 0
Circle Finalizer: Center = [0, 0]; Radius = 0
Point Finalizer: Center = [0, 0]; Radius = 0
Circle Finalizer: Center = [0, 0]; Radius = 0
Point Finalizer: Center = [0, 0]; Radius = 0








7.11.Finalize
7.11.1.Override Finalize method
7.11.2.Display order in which base-class and derived-class constructors and finalizers are called