Using in C #

Source: Internet
Author: User

The using command has two purposes:
The type can be used in the namespace, so that you do not have to limit the type used in the namespace.
Create an alias for a namespace.
The using keyword is also used to create a using statement to define a range where one or more objects will be released.
See using statements. Http://www.yaosansi.com/blog/article.asp? Id = 669
Using namespace;
Using alias = type | namespace;
Parameters
Alias
User-Defined symbols that you want to use to represent namespaces or types. Then, you can use alias to represent the namespace name.
Type
The type you want to use alias.
Namespace
The namespace you want to use alias. Or a namespace that contains the type you want to use without specifying a fully qualified name.
Remarks
The range of the using command is limited to the files containing it.
Create a using alias to limit the identifier to a namespace or type.
Create the using command to use the type in the namespace without specifying the namespace. The using command does not provide you with access to any namespace nested in the specified namespace.
Namespaces are divided into user-defined namespaces and system-defined namespaces. A user-defined namespace is a namespace defined in the Code. To view the list of system-defined namespaces, see. NET Framework class library reference.
For examples of referencing methods in other assembly, see create and use C # DLL.
Example 1
Description
The following example shows how to define a namespace and use a using alias:
Code
Using MyAlias = MyCompany. Proj. Nested;
// Define an alias to represent a namespace.
Namespace MyCompany. Proj
{
Public class MyClass
{
Public static void DoNothing ()
{
}
}
} Example 2
Description
The following example shows how to define the using command and using alias for the class:
Code
// Cs_using_directive2.cs
// Using directive.
Using System;
// Using alias for a class.
Using AliasToMyClass = NameSpace1.MyClass;
Namespace NameSpace1
{
Public class MyClass
{
Public override string ToString ()
{
Return "You are in NameSpace1.MyClass ";
}
}
}
Namespace NameSpace2
{
Class MyClass
{
}
}
Namespace NameSpace3
{
// Using directive:
Using NameSpace1;
// Using directive:
Using NameSpace2;
Class MainClass
{
Static void Main ()
{
AliasToMyClass somevar = new AliasToMyClass ();
Console. WriteLine (somevar );
}
}
} Output
You are in NameSpace1.MyClass
Related Article

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.