- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How many destructors can we have in one class in C#?
In c#, Destructor is a special method of a class and it is used inside a class to destroy the object or instances of classes.
There can be only one destructor inside a class
Following are the properties of destructor in c#
Destructors will not take any parameters
Destructors will begin with a tilde symbol(~)
Destructors (~) cannot be defined in Structs.
Destructor cannot be called. They are invoked automatically.
Destructor implicitly calls Finalize on the base class of object.
Example
class Demo{ ~Demo(){ //Finalizer // cleanup statements... } } class Program{ static void Main(){ Console.ReadLine(); } }
The finalizer implicitly calls Finalize on the base class of the object. Therefore, a call to a finalizer is implicitly translated to the following code −
protected override void Finalize(){ try{ // Cleanup statements... } finally{ base.Finalize(); } }
The programmer has no control over when the finalizer is called
If we declare more than one destructor then the compiler will throw an error.
'Demo' already defines a member called '~Demo'
class Demo{ ~Demo(){ } ~Demo(){ } } class Program{ static void Main(){ Console.ReadKey(); } }
To Continue Learning Please Login
Login with Google