×

VB.Net Programs

VB.Net program to demonstrate the destructor

By Nidhi Last Updated : November 11, 2024

Destructor Example in VB.Net

Here, we will create a Sample class that contains the default constructor and destructor.

Program/Source Code:

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

VB.Net code to demonstrate the example of destructor

'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
Press any key to continue . . .

Explanation:

In the above program, we created a module Module1. Here, we created a class Sample that contains a data member num. The Sample class contains a constructor and destructor.

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

The Main() method is the entry point for the program, here we created an object of Sample class and then called SayHello() method, it will print "Hello world" message on the console screen. Then destructor gets called automatically.

VB.Net Basic Programs »


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.