C # keyword--using

Source: Internet
Author: User
Tags finally block

The using keyword has two main uses:
(i). As an instruction, use to create aliases for namespaces or to import types defined in other namespaces.

(b). As a statement that defines a range, the object is disposed at the end of this range.

using directives
① allows a type to be used in a namespace, so that you do not have to qualify the use of a type in that namespace:
Using System.Text;
Using Pc.company;
② creates an alias for a namespace or type.
Using MyCompany = Pc.company; The alias name of the namespace.
Using Project = PC.Company.Project; Alias of type
Using the introduction of a namespace does not equal 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 on 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 introduction namespace is located. 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.
Another important reason for creating aliases is that the different namespaces introduced in the same file include the same name types, 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.

using Statement
The using statement allows the programmer to specify when an object that uses resources 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.
① can declare objects within a using statement.
Font font2 = new Font ("Arial", 10.0f);
using (Font2)
{
Use Font2
}
② can declare an object before the using statement.
using (font font2 = new Font ("Arial", 10.0f))
{
Use Font2
}
③ can have more than one object to use with a using statement, but these objects must be declared inside a using statement.
using (Font font3=new font ("Arial", 10.0f), font4=new font ("Arial", 10.0f))
{
Use Font3 and font4.
}

Usage rules
①using can only be used to implement the type of the IDisposable interface, the use statement is prohibited for types that do not support the IDisposable interface, or a compilation error occurs;
The ②using statement is useful 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 will not be freed;
The ③using statement supports initializing multiple variables, but only if 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 initialization of variables of different types, for example:
using (IDisposable font = new Font ("Verdana", "a"), pen = new Pen (brushes.black))
{
Float size = (font as font). Size;
Brush brush = (pen as pen). Brush;
}

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

C # keyword--using

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.