Difference Using Directive and Statement

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();
}
}


Similar Articles