public class Hello
{
public static void Main()
{
AliasTest.TestFunction(); // using the alias
}
}
Programming Resource Downloads |
Now let us take a look at the using statement
The .NET Framework uses a system called reference-tracing garbage collection which
periodically performs all of the necessary memory management tasks.
When you create objects that encapsulate unmanaged resources, you must explicitly
release the unmanaged resources when you are finished using them in your application.
The most common type of unmanaged resource is an object that wraps an operating
system resource, such as a file, window, or network connection. Although the garbage
collector is able to track the lifetime of an object that encapsulates an unmanaged
resource, it does not have specific knowledge about how to clean up the resource.
For these types of objects, the .NET Framework provides the Object.Finalize Method,
which allows an object to clean up its unmanaged resources properly when the garbage
collector reclaims the memory used by the object.
An object's resources can be cleaned up when
(a) the object leaves the scope or when it's lifetime is over.
(b) the object is no longer referenced.
We can't determine when the .NET Framework will execute the Finilize method of
that object. We only know that the system will call the Finilize method some time
after when, any of the above two conditions are met.
Garbage Collection has an advantage because it is automatic. It is also has a
disadvantage in that it can't be initiated directly by the applications and some
resources may be active for a longer time, then expected. The class can manage
the system resources in a beter way if an additional destructor named Dispose
is implemented using the IDisposable interface. You must explicitly call Dispose
when an object is no longer needed. Dispose method should release all of the resources
that it owns. A Dispose method should call the GC.SuppressFinalize Method for
the object it is disposing. If the object is currently on the finalization queue,
GC.SuppressFinalize prevents its Finalize method from being called because implementing
Finalize methods or destructors can have a negative impact on performance. Objects
with Finalize methods also have a slightly higher cost per allocation because
an extra entry must be made in the finalization queue; so, if the Dispose method
has already done the work to clean up the object, then
it is not necessary for the garbage collector to call the object's Finalize method.
The using statement defines a scope at the end of which an object will be disposed.
By creating an instance of a class in a using statement we make sure that Dispose
method is called on the object when the using statement is exited. A using statement
can be exited either when the end of the
using statement is reached or if, an exception is thrown and control leaves the
statement block before the end of the statement.
The following example illustrates the implementation of the Dispose method.
using System;
public class Test : IDisposable
{
//Constructor method for the class
public Test()
{
Console.WriteLine( "Calling the Constructor Method...." );
}
//Finalization method for the class
~Test()
{
Console.WriteLine( "Calling the Finalization Method...." );
}
public void Hello()
{
Console.WriteLine( "Hello Everyone." );
}
//Dispose method for the class
public void Dispose()
{
Console.WriteLine( "Calling the Dispose Method..." );
//Supressing the Finalization method call
GC.SuppressFinalize(this);
}
}
public class TestMain
{
static public void Main()
{
using (Test t = new Test() )
{
t.Hello();
}
}
}
Article originally appeared at C-Sharp
Corner
About
the Author:
Yoganand did his Bachelors degree in Physics from Saurashtra University,
Rajkot (India) , then studied Application programing at Datapro, Baroda (India).
I have been in the development side for the past 6 years. Started my career as
a Clipper 5.2 programmer, then moved to Delphi, and never looked back to any other
programming language. This is the first time after many years switching to some
other language .i.e C#. I am from Ahmedabad (India), but presently working with
Derivco (http://www.microgaming.com),
Durban (South Africa) as a software developer. email : yogia@microgaming.com
Read this Newsletter at: http://www.cprogrammingtrends.com/2003/0620.html |
|