Wednesday 6 March 2013

Methods of a 'class' in CSharp

A class is a collection of members, where members of class can be off various kinds like :
  • Fields
  • Members
  • Constructors
  • Destructors
  • Properties
  • Indexes
  • Events
  • Delegates
  • Enum
Destructor: This is also a special method present under a child class same like a constructor but constructor method gets called when object of class is created and destructor is called whenobject of class is destroyed.
   Both constructor and destructor method will exactly have the same name i.e the name of the class which they have. To differentiate between this two we use 'tidle' operation before the destructor method.

class Test
 {
    Test() 
     {
        //Constructors
     } 
   ~Test()
    {
      //Destructors
    }
}
  • Destructor methods cannot be applied with any modifirs or parameters.
  • Garbage Collector is responsible for destroying the object of class and when the object of class is being destroyed, destructor methods gets called and moreover Garbage Collector can destroy the object of class in any of the following three classes.
  1. In the end of a program's execution each and every object i.e associated with the program is automatically destroyed.
  2. Sometime in the middle of a programs execution implicit calling of Garbage Collector takes place provided the memory is full that it identifies finds unused object and destroys them.