Destructors

  • Destructors are the special methods that r used to release the instance of the class from memory.
  • The main purpose of it is to perform the memory clean up action. the programmer can not have any control to call the destructor.
  • The .Net frame work automatically runs the destructor to destroy objects in the memory.
  • A single class can have only one destructor.

Declaration of Destructor

  • Destructor follows the same rules as that of constructors but the difference comes in the functioning.
  • Destructor has same name as class name as its class but is prefixed with a '~' called as tilde operator.
  • Destructors can not be inherited or overloaded.
example:

using System;

class Addition
{
static int n1,n2;
int total;
}

Addition()
{
n1=5;
n2=3;
total=0;
}
public void operation()
{
total=n1+n2;
Console.WriteLine("The result is {0}", total);
}
~Addition()/*.............. Destructor.............*/
{
Console.WriteLine("Destructor invoked");
}
static void Main(String [] arg)
{
Addition obj=new Addition();
obj.operation();
}
}

note: Even when u dont call the destructor, garbage collector releases the memory.

Garbage Collection

  • It is a process that automatically frees the memory space which is used for the objects at the time of program execution.
  • The memory for the object is freed only after observing the status of the object (i.e when object is no longer in use).
  • The decision to invoke the destructor is made by a special program of C# called as garbage collector.
  • In C#, you can not destroy an object explicitly in code, in fact it has the feature of garbage collection which destroys the objects for the programmers.
  • Garbage collection mainly involves,
  1. objects get destroyed: doesnt specify when the object is destroyed.
  2. Only unused objects are destroyed: object is nt deleted unless it holds any reference.
  • C# uses two special methods to release the instance of a class from memory
  1. Finalize()
  2. Dispose()
  • Finalize destructor is a special method that is called from the class to which it belongs or from the derived class.
  • It is called only after the last reference of the object is released from the memory.
  • .Net frame work automatically runs the Finalize() destructor to destroy objects in the memory. and keep in mind that the Finalize method does nt invoke immediately.
  • Dispose() method is called to release a resource, such as a database connection as soon as the object using such a resource is no longer in use.
  • Unlike the Finalize() destructor , the Dispose() method is not called automatically, and you must explicitly call it from the client application when an object is no longer needed.
  • IDisposable interface contains the Dispose() method. So in order to invoke the Dispose() method a class must implement the IDisposable interface.

Constructors

  • These form the special member functions of the class .
  • These have the same name as class name and constructors do not return any value .
  • A Constructor is complimentary to a destructor. It is a good programming to use both constructors as well as destructors.
  • Constructors can be defined in two ways as
1. static Constructors.
2.Instance Constructors.


Static Constructors:
  • These r used to intialise static variables of a class.
  • These variables r created using static key word and they store values that can be shared by all the instances of a class .
  • Static constructors have an implict private access.
  • These ill be invoked only once during execution.
  • exam:

    public class Example
    {

    static int n1;
    int n2;

    static Example()
    {
    n1=5; /*.................. accepts the value for variable n1............................*/

    n2=10; /.......................... puts an error msg...........*/
    }
    }

note: We use constructors to intialise data members and not for any input or output operations.
and we can have more than one constructor for a class.

Constructors with parameters

At times user wants to pass values to variables at run time where it cant be possible through intializing them at the starting of prog, so during this situation he can make use of passing the values to variables through parameters.
constructors can be used in order to supply values to variables at run time. ......

here is an exam program to illustrate the process of functioning.........

using System;

namespace arithemetic_oper
{
class Addition
{
static int num1,num2;
int total;

Addition(int n1,int n2)
{
num1=n1;
num2=n2;
}

public void addNum()
{
total=num1+num2;
}

public void displayNum()
{
Console.WriteLine(" Result is : {0}",total);
}

public static void Main(String [] arg)
{
int var1,var2;
Console.WriteLine("Enter the var1:\t");
var1= Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter the var2:\t");
var2=Convert.ToInt32(Console.ReadLine());

Addition obj=new Addition(var1,var2);

obj.addNum();
obj.displayNum();
Console.ReadLine();
}
}
}

Instance Constructors:

  • Instance constructor is called when ever instance of a class is created.
  • These r used to intialize data members of the class.
  • Constructors do not return any values.
  • If there r no constructors for a class, then compiler creates a default constructor for itself.



Newer Posts Older Posts Home