Monday, May 27, 2013

Destructors

Destructors are used to free unused resources and release memory used by the resources. Destruction of Managed objects are handled by the Garbage collector in the .Net Framework, we can use the destructor to release any unmanaged resources like Network connectivity, database connections etc and release the resources.

The signature of the destructor is similar to a constructor, it shares the same name of the class, the only difference is that we add a ~ symbol in front of the method name to denote that this is a destructor.

The following example defines a destructor for the CEmployee class

    class CEmployee
    {
        public int id;
        public string Name;
        //
        public CEmployee()
        {
        }
        //
        ~CEmployee()
        {
            //Destroy objects here.
        }
    }

There can be only one destructor for a class.

Search Flipkart Products:
Flipkart.com

No comments: