|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionIn the enterprise application development world, the buzzwords are Performance, Scalability, and Security. I started my career as a VC++ programmer, and one fine morning, I was transferred to Web development department. Like every C++ programmer, I also was frustrated. I thought every Tom, Dick, and our very own Harry can program in HTML. But, soon I found that the real challenge is to produce high performance, scalable, and reliable applications. And above all that the loosely coupled, stateless nature of web environment is always going to haunt you. In order to produce high performance scalable applications, it is important to use your resources in an optimized manner. One tip is that use your resource as late as you can and free it at the earliest after your use. My intention here is to describe the object cleaning up mechanism used in C#. DestructorsAs we all know, ‘Destructors’ are used to destruct instances of classes. When we are using destructors in C#, we have to keep in mind the following things:
The following is a declaration of a destructor for the class ~ 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 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 using System;
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 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:
Calling the garbage collectorYou can force the garbage collector to do clean up by calling the Implement IDisposable interface.The class MyClass:IDisposable
{
public void Dispose()
{
//implementation
}
}
When we implement Using the Destructor and IDisposable interface togetherPublic class MyClass:IDisposable
{
private bool IsDisposed=false;
public void Dispose()
{
Dispose(true);
GC.SupressFinalize(this);
}
protected void Dispose(bool Diposing)
{
if(!IsDisposed)
{
if(Disposing)
{
//Clean Up managed resources
}
//Clean up unmanaged resources
}
IsDisposed=true;
}
~MyClass()
{
Dispose(false);
}
}
Here the overload of When a client calls Notice that in the above example, the destructor calls the ConclusionEven though we have spent some time implementing the using (MyClass objCls =new MyClass())
{
}
When the control exits from the
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||