Destructors in C#

Posted on Updated on

Image result for Destructors in C#Destructors in C#

For high performance scalable application, the resources must be used in optimized manner. The resources must be used as late as possible and must be freed as early as possible after completion of the work. They are mainly used to destroy the instance of the class.

Properties of C#

  • Only one destructor is possible in a class.
  • It is not possible to inherit or overload a destructor.
  • They are invoked automatically whenever objects are not in use.
  • They don’t have any access modifier or parameters.
  • Destructors cannot be defined in structures.
  • They always have the same name as class name
  • The destructor name always starts with tilde(“~”) sign.

Syntax:

class Car

    {

        ~Car()  // destructor

        {

            // cleanup statements…

        }

    }

The destructor implicitly calls Object. Finalize of the base class. Therefore, the previous destructor code is implicitly translated to the following code:

protected override void Finalize()
{
try
{
// Cleanup statements…
}
finally
{
base.Finalize();
}
}

Image result for Destructors in C#

Because of this the Finalize() method is called recursively for all the instances from most derived to least derived. It is advised not to create an empty destructor because whenever a destructor is called the garbage collector is invoked to process the queue which causes loss of performance.

Destructor is called by the garbage collector, so when it will be called can’t be controlled by the programmer. It is the responsibility of the garbage collector to check out which objects are not required by the application. If it finds that the object is eligible for destruction, it calls the destructor and retrieves the memory used to restore objects. They are also called when the program exits.

Example

using System;

class A

{

 public A()

 {

  Console.WriteLine(“Creating A”);

 }

 ~A()

 {

  Console.WriteLine(“Destroying A”);

 }

}

class B:A

{

 public B()

 {

  Console.WriteLine(“Creating B”);

 }

 ~B()

 {

  Console.WriteLine(“Destroying B”);

 }

}

class C:B

{

 public C()

 {

  Console.WriteLine(“Creating C”);

 }

 ~C()

 {

  Console.WriteLine(“Destroying C”);

 }

}

class App

{

 public static void Main()

 {

  C c=new C();

  Console.WriteLine(“Object Created “);

  Console.WriteLine(“Press enter to Destroy it”);

  Console.ReadLine();

  c=null;

  //GC.Collect();

  Console.Read();

 }

}

Output:

Creating A

Creating B

Creating C

Object Created

Press enter to Destroy it

As it is expected that after calling constructors, object will be created and the message ‘press enter to destroy it’ will be executed and after that the statement defined in destructor will be executed. But it doesn’t happen so as the messages of destructor are not displayed because the destructor is called when the program exits. This can be checked by redirecting the output to the text file.

If it is required to call the destructor, when the use of object is finished, there are two ways:

  • Call the garbage collector to clean up.
  • Implement the Dispose method of IDisposable interface.

Call the garbage collector

A program cannot call the garbage collector as they are invoked automatically but they can be forced by using GC.Collect() method but it is recommended to avoid such calling because of the performance issues.

Implement IDisposable interface

IDisposable interface has only one public method having the signature void Dispose(). This method can be used to close or release unmanaged resources such as files and streams. This method is used for freeing all the resources held by the object.

class MyClass:IDisposable{    public void Dispose() {  //implementation }}

*****************************************************************************************************

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.
Helpline: 9814666333, 8699444666
Email: info@technocampus.co.in

Please fill the form and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s