SIGN UP MEMBER LOGIN:    
ARTICLE

Difference Using Directive and Statement

Posted by Yoganand Aiyadurai Articles | C# Language July 26, 2001
This article explains difference between using directive and using statement with sample example.
Reader Level:

The using directive is used for FAQ: C# and .NET

  1. creating an alias for a class or a namespace.
  2. permiting the use of types/classes in a namespace without having to specify the namespace.  

The following sample shows how to define a using for both of the above cases:

using
System; //using a system defined namespace
using AliasTest = Test; // defing an alias to represent a class
public class Test
{
public static void TestFunction()
{
Console.WriteLine("TestFunction() called from class Test.");
//The System namespace contains the class Console
}
}
public class Hello
{
public static void Main()
{
AliasTest.TestFunction();
// using the alias
}
}

Now let us take a look at the using statement

The .NET Framework uses a system called reference-tracing garbage collection which perodically performs all of the necessary memory management task.

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 is an advantage because it is automatic. It is also an disadvantage because 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();
}
}

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Become a Sponsor