Home >>VB.Net Built-in Functions >VB.Net Program to demonstrate the destructor

VB.Net Program to demonstrate the destructor

Write a VB.Net Program to demonstrate the destructor

Here, we can build a class of samples the default constructor and destructor.

Program :

The source code is given below to demonstrate the destructor. The program given is compiled and successfully executed.

'VB.net program to demonstrate the destructor.


Module Module1
    Class Sample
        Public Sub New()
            Console.WriteLine("Constructor called")
        End Sub
        Protected Overrides Sub Finalize()
            Console.WriteLine("Destructor called")
        End Sub
        Public Sub SayHello()
            Console.WriteLine("Hello World")
        End Sub
    End Class
    Sub Main()
        Dim obj As New Sample()
        obj.SayHello()
    End Sub
End Module
	
Output:
Constructor called
Hello World
Destructor called
Explanation:

We created a Module1 module in the program above. Here, a class sample containing a data member num has been developed. A constructor and a destructure are included in the Sample class.

Here, using the finalize() method, we implemented the destructor.

The Main() method is the program entry point, we created a Sample Class object here and then named the SayHello() method, it will print the "Hello world" message on the console screen. Then the destructor immediately gets called.


No Sidebar ads