The using directive is used for:
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:
-==-
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.
-==-
Article originally appeared at C-Sharp Corner
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