Constuctor and Destructor in C sharp

Posted: February 5, 2013 in Object Oriented concept Via C#
Tags: , ,

hello !!

This detailed article will let u know about how constructors and destructors work in C# and how to use them in your applications.

CONSTRUCTOR:

Whenever a class or struct is created a constructor is called.A class or struct may have multiple constructors that take different arguments.

They are used to initialize the object.constructor must have the same name as that of class. They can contain parameters.

A constructor that takes no parameters is called a default constructor. Default constructors are invoked whenever an object is instantiated using the new operator and no arguments are provided to new.

look at the following code.

using System;

class Rectangle{
   public int Width;
   public int Height;
   public Rectangle(int w, int h){  ---> constructor containing parameters.
      Width = w;
      Height = h;
   }
}

class Program
{

    static void Main(string[] args)
    {

        Rectangle r = new Rectangle(1, 2);

        Console.WriteLine(r.Width)

output : 1 

In the above code , we have simply implemented the constructor in the class Rectangle.

Constructor can have following modifier:
  • public
  • internal
  • private
  • protected
  • unsafe
  • extern
Now , we will look at constructor overloading.
constructor overloading is basically, the implementation of static polymorphism.


using System;

class Rectangle {
   public int Width;
   public int Height;

   public Rectangle(){   -----> constructor A with no parameter 
      Width  = 0;
      Height = 0;
   }
   public Rectangle(int w, int h){-----> constructor B with TWO parameter
}   
}

class Program
{

    static void Main(string[] args)
    {

        Rectangle r = new Rectangle();
        Console.WriteLine(r.Width);

        Rectangle r2 = new Rectangle(2, 3);
        Console.WriteLine(r.Width);
    }
Similarly, we can implement this with using three or more constructor. A constructor can be used, where every time an object gets created and if we want some code to be executed automatically. The code that we want to execute must be put in the constructor.

 NOTE :CONSTRUCTOR CAN NOT BE INHERITED

DESTRUCTOR:

The .NET framework has an in built mechanism called Garbage Collection to de-allocate memory occupied by the un-used objects. The destructor implements the statements to be executed during the garbage collection process. A destructor is a function with the same name as the name of the class but starting with the character ~.

Example:

class Complex
{
public Complex()
{
// constructor
}
~Complex()
{
// Destructor
}
}

Remember that a destructor can’t have any modifiers like private, public etc. If we declare a destructor with a modifier, the compiler will show an error.Also destructor will come in only one form, without any arguments. There is no parameterized destructor in C#. 

Destructors are invoked automatically and can’t be invoked explicitly. An object becomes eligible for garbage collection, when it is no longer used by the active part of the program. Execution of destructor may occur at any time after the instance or object becomes eligible for destruction. 

In C# all classes are implicitly derived from the super base class object. The object class contains one special method, Finalize(), which every class can override. The Garbage Collection mechanism in .NET will call this method prior to the garbage collection of the objects this class. Remember when we provide a destructor in a class, during the compilation time, the compiler automatically generates the Finalize() method. That means that a destructor and overridden Finalize() method can’t co-exist in a class. The following code will generate a compilation error because of the above program

class Complex
{
~Complex()
{
}
protected override void Finaliz()
{
}
}

so that all about constructor and destructor in c sharp 🙂

HAPPY CODING !!!

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