C # using statement + Using Keyword

Source: Internet
Author: User

Using statement

The Using statement obtains one or more resources, executes a statement, and then disposes of the resource. A using statement is translated into three parts: acquisition, usage, and disposal.

Example:

Using (font font1 = new font ("Arial", 10.0f ))
{
}

(

Note: It's different from a direct block wrapping:

{

Font font1 = new font ();

Statements;

}

For using statement, it will finally call dispose () of the font while the direct BLOCK statement will do nothing for managed code here as GC will decide when to delete the GDI font object.

)

 

C # compiler will translate it to be:

{
Font font1 = new font ("Arial", 10.0f );
Try {
Statement;
}
Finally {
If (font1! = NULL) (idisposable) font1). Dispose ();
}
}

Using Keyword

TheUsingKeyword has two uses:

Using alias directives

A using-alias-directive introduces an identifier that serves as an alias for a namespace or type within the immediately enclosing compilation unit or namespace body.

 

Using identifier = namespace-or-type-name;

Within member declarations in a compilation unit or namespace body that contains a using-alias-Directive, the identifier introduced by the using-alias-directive can be used to reference the given namespace or type.

For example:

Namespace n1.n2
{
Class {}
}
Namespace N3
{
Using a = n1.n2.;
Class B: {}
}

Here, within member declarations in the N3 namespace, A is an alias for n1.n2. A, and thus class n3. B
Derives from class n1.n2. A. The same effect can be obtained by creating an alias R for n1.n2 and then
Referencing r.a:

 

Namespace N3
{
Using r = n1.n2;
Class B: r.a {}
}

The identifier of a using-alias-Directive must be unique within the Declaration space of the compilation unit or namespace that immediately contains the using-alias-directive.

Using namespace directives

A using-namespace-Directive imports the types contained in a namespace into the immediately enclosing compilation unit or namespace body, enabling the identifier of each type to be used without qualification.

 

Using-namespace-Directive: Using namespace-name;

Within member declarations in compilation unit or namespace body that contains a using-namespace-Directive, the types contained in the given namespace can be referenced directly.

For example:

Namespace n1.n2
{
Class {}
}
Namespace N3
{
Using n1.n2;
Class B: {}
}

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.