[Reserved] C # keywords – using

November 18, 2011
using keyword has two main purposes:
(a ) as instructions for creating an alias for a namespace or import other types defined in a namespace.
(b) As a statement, used to define a range, in this range will be released at the end of the object.
using directive
① allow the use of types in a namespace, so you do not have space in the name of a limited a type of use:
using System.Text;
using PC.Company;
< br /> ② To create an alias for a namespace or type.
using MyCompany = PC.Company; / / namespace alias.
using Project = PC.Company.Project; / / type of alias
using namespace introduced, does not mean that the compiler compiled with the set the namespace where the assembly, assembly process of the load depends on the existence of an assembly operation is invoked , if there is no code, the compiler will call the action does not load using the namespace where the introduction of the assembly. Therefore, beginning in the source file, the introduction of multiple namespaces, not loading multiple assemblies, will not cause “excessive reference” of the state.
create an alias for another important reason is that the same file into a different namespace includes the type of the same name, such as SharpMap.Geometries.Point with System.Drawing.Point. To avoid name conflicts can be resolved by setting an alias:
using SGPoint = SharpMap.Geometries.Point;
using SDPoint = System.Drawing.Point;
Although we can type the whole name to be distinguished, but this is clearly not the best solution. Create an alias with the using directive, effective solution to this potential naming conflicts, is the best solution.
using statement
using statement allows the programmer to specify the use of resources when the object should release resources. objects used in a using statement must implement the IDisposable interface. This interface provides the Dispose method, which will release resources for this object.
① being declared in the using statement object.
Font font2 = new Font (“Arial”, 10.0f);
using (font2)
{

/ / use font2
}
② in the using statement before the statement object.
using (Font font2 = new Font (“Arial”, 10.0f))
{
/ / use font2 < br />
}
③ Multiple objects can be used together with the using statement, but must be declared inside the using statement in these objects.
using (Font font3 = new Font (“Arial”, 10.0f), font4 = new Font (“Arial”, 10.0f))
{
/ / Use font3 and font4.
}
use rules
① using can only be used to implement the IDisposable interface type, to prohibit the type does not support the IDisposable interface using statement, otherwise it will appear compile error ;
using statement for clean-up individual unmanaged resources, and more to clean up the unmanaged object is best to try-finnaly to achieve, because there may be nested using statement to hide the Bug. inner using block throws an exception, will not block the release of the outer layer of the object using resources;
③ using multiple variable initialization statement to support, but only the type of these variables must be the same For example:
using (Pen p1 = new Pen (Brushes.Black), p2 = new Pen (Brushes.Blue))
{

/ /
}
④ for the initialization of the different types of variables can be declared as IDisposable type are, for example:
using (IDisposable font = new Font (“Verdana”, 12), pen = new Pen (Brushes.Black))
{
float size = (font as Font) . Size;
Brush brush = (pen as Pen). Brush;
}
using real
at the assembly stage, the compiler will automatically be using try-finally statement is generated as a statement in the finally block and call the object Dispose method to clean up resources. Therefore, using try-finally statement is equivalent to the statement, for example:
Font f2 = new Font (“Arial”, 10, FontStyle.Bold);
try
{
/ / perform text drawing operations
}
finally
{
if (f2! = null) ((IDisposable) f2). Dispose ();
}

Posted: January 3rd, 2012
at 2:24pm by admin

Tagged with


Categories: Uncategorized

Comments: No comments