Objects end life after they leave scope and are released by the common language runtime (CLR). Visual Basic .NET controls the release of system resources using procedures called destructors. Together, constructors (special methods that allow control over initialization) and destructors support the creation of robust and predictable class libraries.
Sub New and Sub Finalize
The Sub New and Sub Finalize procedures in Visual Basic .NET initialize and destroy objects; they replace the Class_Initialize and Class_Terminate methods used in previous versions of Visual Basic. Unlike Class_Initialize, the Sub New constructor can run only once when a class is created, and it cannot be called explicitly anywhere other than in the first line of code in another constructor from either the same class or from a derived class. Furthermore, the code in the Sub New method always runs before any other code in a class. Visual Basic .NET implicitly creates a Sub New constructor at run time if you do not explicitly define a Sub New procedure for a class.
Before releasing objects, the CLR automatically calls the Finalize method for objects that define a Sub Finalize procedure. The Finalize method can contain code that needs to execute just before an object is destroyed, such as closing files and saving state information. There is a slight performance penalty for executing Sub Finalize, so you should define a Sub Finalize method only when you need to release objects explicitly.
The Finalize destructor is a protected method that can be called only from the class it belongs to, or from derived classes. The system calls Finalize automatically when an object is destroyed, so you should not explicitly call Finalize from outside of a derived class's Finalize implementation. Unlike Class_Terminate, which executed as soon as an object was set to nothing, there is usually a delay between when an object loses scope and when Visual Basic .NET calls the Finalize destructor. Visual Basic .NET allows for a second kind of destructor, named Dispose, which can be explicitly called at any time to release resources immediately.

Join the conversation! Your thoughts help the community grow.