C # using Keyword Summary

Source: Internet
Author: User
Tags finally block

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
   

①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 when initializing multiple 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;
}

3, the using statementSubstance


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 # using Keyword summary (GO)

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.