One, using as a directive, has the following two functions
1. Import a type defined in another namespace or namespace so that you do not have to use a fully qualified name for the type .
Note: (1) using the introduction of a namespace does not mean that the assembly in which the namespace is loaded when the compiler compiles, the load of the assembly is determined by whether there is a call operation to the Assembly in the program, and if no call operation exists in the code, the compiler will not load the assembly in which the using introduced the namespace. Therefore, the introduction of multiple namespaces at the beginning of the source file, rather than loading multiple assemblies, does not cause the "over-referencing" disadvantage.
(2) With the Use System directive, an interesting result is that the string data type can be represented by a different case form: string or String. The previous version relies on the using System directive, and the latter version uses the string keyword. Both of them legitimately refer to the System.String data type in C #, and the resulting CIL code is no different.
(3) The using directive can be declared not only at the top of the file, but also at the top of the namespace declaration.
Example: namespace Awl.Michaelis.EssentialCSharp
{using System;
.............
}
The difference between the two is that the using directive of the latter is only valid within the namespace that you declare. For example, if a new namespace is declared outside the namespace Awl.Michaelis.EssentialCSharp, the new namespace is not affected by the using system directives.
2. Create an alias name for the namespace or type.
Example: Using MyCompany = Pc.company; The alias name of the namespace.
Using Project = PC.Company.Project; Alias of type
Note: Creating aliases for namespaces or types typically has the following two reasons:
(1) abbreviation for a long name.
(2) Eliminate the ambiguity of two types with the same name.
For example, the different namespaces introduced in the same file include types with the same name, such as SharpMap.Geometries.Point and System.Drawing.Point. To avoid name collisions, you can set up aliases to resolve:
Using Sgpoint = SharpMap.Geometries.Point;
Using Sdpoint = System.Drawing.Point;
Although we can differentiate by type full name, this is clearly not the best solution. Creating aliases with using directives effectively resolves this possible naming conflict and is the best solution.
A using as a statement that defines a range in which the object is disposed at the end of the range.
1. How to use
The
using statement allows a programmer to specify when an object using a resource should release resources. The object used in the using statement must implement the IDisposable interface. This interface provides the Dispose method, which frees the resources for this object. The
① can declare an object before the using statement.
Font font2 = new Font ("Arial", 10.0f);
using (font2)
{
//Use fo Nt2
}
② can declare objects within a using statement.
using (font font2 = new Font ("Arial", 10.0f))
{
   &N bsp; //Use Font2
}
③ can have multiple objects used with the using statement, but must be in the using statement Internally declare these objects.
using (Font font3=new font ("Arial", 10.0f), font4=new font ("Arial", 10.0f)
{
//Use Font3 and font4.
2, Considerations
The
①using can only be used to implement the type of the IDisposable interface, and a using statement is prohibited for types that do not support IDisposable interfaces. Otherwise, a compilation error occurs, and the
②using statement is appropriate for cleaning up a single unmanaged resource, while the cleanup of multiple unmanaged objects is best implemented with try-finnaly because there may be a hidden bug in nested using statements. When an inner using block throws an exception, the object resource of the outer using block cannot be freed; the
③using statement supports initialization of multiple variables, provided that the types of these variables must be the same, for example:
using (Pen p1 = new Pen (brushes.black), p2 = new Pen (brushes.blue))
{
; //
}
④ can be declared as a IDisposable type for initializing multiple variables of different types, for example:
using (IDisposable font = New Font ("Verdana", pen = new Pen (brushes.black))
{
; Float size = (font as font). Size;
Brush brush = (pen as pen). Brush;
}
3, using statement Essence
During the program compilation phase, the compiler automatically builds the using statement into a try-finally statement and calls the object's Dispose method in the finally block to clean up the resource. Therefore, the using statement is equivalent to the try-finally statement, for example:
Font F2 = new Font ("Arial", ten, FontStyle.Bold);
Try
{
Perform text drawing operations
}
Finally
{
if (F2! = null) ((IDisposable) F2). Dispose ();
}
"Reprint" Using keyword Summary