Saturday, January 17, 2015

2015: Intw Que 1- Constructors and Destructors

Difference between Constructors & Descturctors

Constructors:
  • Constructor is Used to Initialize the Object.
  • Constructor can takes arguments.
  • Constructor overloading can be possible means more than one constructors can be defined insame class.
  • Constructor has same name as class name.
  • Syntax of constructor:
                class class_name
                  {
                     class_name(){}
                     class_name(argulist){}
                 } ;
  • Constructor are of following:
               1)Default Constructor.
               2)Parameterized Constructor.
               3)Copy Constructor.

Destructors:

  • ‘Destructors’ are used to destruct instances of classes.
  • 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.
The following is a declaration of a destructor for the class MyClass:

~ MyClass() 
{
   // Cleaning up code goes here
}
  • The programmer has no control on when the destructor is going to be executed because this is determined by the Garbage Collector. The garbage collector checks for objects that are no longer being used by the application. It considers these objects eligible for destruction and reclaims their memory. 
  • Destructors are also called when the program exits. When a destructor executes what is happening behind the scenes is that the destructor implicitly calls the Object.Finalize method on the object's base class. Therefore, the preceding destructor code is implicitly translated to:
protected override void Finalize()
{
   try
   {
      // Cleaning up .
   }
   finally
   {
      base.Finalize();
   }
}


Now, let us look at an example of how destructors are called. We have three classes AB and CB is derived from A, and C is derived from B. Each class has their own constructors and destructors. In the main of the class App, we create an object of C

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();
 }

}

As we expect, the constructors of base classes will be executed and program will wait for the user to press 'enter'. When this occurs, we set the object of class C to null. But the destructors are not executing ..!!?? As we already said, the programmer has no control on when the destructor is going to be executed because the Garbage Collector determines this. But the destructors are called when the program exits. You can check this by redirecting the o/p of the program to a text file. I have the output here. Notice that the destructors of the base classes are called because behind the scenes base.Finalize() is called.

Creating A
Creating B
Creating C
Object Created 
Press enter to Destroy it
Destroying C
Destroying B
Destroying A

So, what do you do if you want to call the destructors once you are finished using the object? There are two ways:
  • Call the Garbage collector to clean up: You can force the garbage collector to do clean up by calling the GC.Collect method, but in most cases, this should be avoided because it may result in performance issues. In the above program, remove the comment onGC.Collect(). Compile and run it. Now, you can see the destructors being executed in the console itself.
  • Implement Dispose method of IDisposable interface: The IDisposable interface contains only one public method with signature void Dispose(). We can implement this method to close or release unmanaged resources such as files, streams, and handles held by an instance of the class that implements this interface.
class MyClass:IDisposable
{
    public void Dispose()
 {
  //implementation
 }
}

No comments:

Post a Comment