- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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(); } }
- Related Articles
- How many levels of pointers can we have in C/C++?
- How can we call one constructor from another in the same class in C#?
- How many ribs do we have?
- Destructors in C++
- C++ program to count how many minutes we have to wait to meet at least one swimmer
- How to subscribe to an Event in C# and can we have multiple subscribers to one Event in a C#?
- How many bones do we have when we are newborns?
- How many tangents can a circle have?
- How many parameters can a lambda expression have in Java?
- Can we declare more than one class in a single Java program?
- How many ways can get the instance of a Class class in Java?
- How we can create singleton class in Python?
- Playing with Destructors in C++
- In how many ways we can concatenate Strings in Java?
- C++ program to check how many students have greater score than first one
