C Sharp Destructor | C# Destructor - c# - c# tutorial - c# net
What is destructor in C#?
- A destructor works opposite to constructor, It destructs the objects of classes.
- It can be defined only once in a class. Like constructors, it is invoked automatically.
- A destructor, a method called once an object is disposed, can be used to cleanup resources used by the object.
- Destructors doesn't look very much like other methods in C#. ... Once the object is collected by the garbage collector, this method is called.
- When we are using destructors in C#, we have to keep in mind the following things:
- A class can only have one destructor.
- Destructors cannot be inherited or overloaded.
- Destructors cannot be called. They are invoked automatically.
- A destructor does not take modifiers or have parameters.
- Note: C# destructor cannot have parameters. Moreover, modifiers can't be applied on destructors.
C# Constructor and Destructor Example
- Let's see an example of constructor and destructor in C# which is called automatically.
C# examples - Output :
- Note: Destructor can't be public. We can't apply any modifier on destructors.