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