The using keyword has two main purposes!

Source: Internet
Author: User

UsingThe keyword has two main purposes:

    • The Using command creates the namespace alias or imports the type defined in other namespaces.

UsingThe command has two purposes:

    • Create a namespace alias (UsingAlias ).
    • Type can be used in a namespace. For example, you do not need to limit the type usage in the namespace (UsingCommand ).
 
Using[Alias =]Class_or_namespace;

Where:

Alias(Optional)
The user-defined symbol of the namespace. Then, you can use AliasThe namespace name.
Class_or_namespace
The namespace name that you want to use or provide aliases for, or the class name that you want to provide aliases.
Remarks

CreateUsingAliases make it easier to restrict identifiers to namespaces or classes.

CreateUsingCommand to use the type in the namespace without specifying the namespace.UsingThe command does not provide you with access to any namespaces that may be nested in the specified namespace.

Namespaces are divided into user-defined namespaces and system-defined namespaces. The user-defined namespace is inCode. For a list of system-defined namespaces, see. NET Framework documentation.

    • The Using statement defines a range where objects are disposed at the end of the range.

Using (Expression|Type Identifier = Initializer)Statement

Where:

Expression
You want to exit UsingStatement call Dispose.
Type
Identifier.
Identifier
TypeType name or identifier. Define more than one TypeType IdentifierYes. In each Identifier = InitializerThere is a comma in front.
Initializer
Create an object expression.
Statement
Embedded statement or the statement to be executed.

InUsingCreate an instance in the statement to exitUsingThe statement is called on the object.Dispose. WhenUsingThe end of the statement, or if an exception is thrown before the statement ends and the exit statement block is controlled, you can exit.UsingStatement.

the instantiated object must implement the system. idisposable interface

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.