Destructors
are used to destruct instances of classes. Destructors are mainly used to clean
the resources.
Syntax
~ClassName(){
}
A
class can only have one destructor. Destructors cannot be called explicitly, they are
invoked automatically. A destructor does not take modifiers or have parameters.
Program.cs
using System; public class Employee { private int id; private String firstName; private String lastName; public Employee(int id, String firstName, String lastName) { Console.WriteLine("Initializing the object"); this.id = id; this.firstName = firstName; this.lastName = lastName; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } ~Employee() { Console.WriteLine("Destructiing the object"); } } class Program { static void Main(string[] args) { Employee emp1 = new Employee(1, "Hari krishna", "Gurram"); } }
Output
Initializing
the object
Destructiing
the object
No comments:
Post a Comment