Three usages of using in C #

Source: Internet
Author: User
Tags define

A using directive has two uses:

    • Allows types to be used in namespaces so that you do not have to qualify the types that are used in that namespace.

    • Create aliases for namespaces.

The using keyword is also used to create a using statement that defines a scope that frees one or more objects from this scope.

See Using statement. http://www.yaosansi.com/blog/article.asp?id=669

Using namespace;using alias = Type|namespace;

Parameters

Alias

A user-defined symbol that you want to represent a namespace or type. You can then use the alias to represent the namespace name.

Type

The type you want to represent with the alias.

Namespace

The namespace that you want to represent through the alias. Or a namespace that contains types that you want to use without specifying a fully qualified name.

Note

The scope of the using directive is limited to the file containing it.

Create a using alias to make it easier to qualify identifiers to namespaces or types.

Create a using directive to use the type in a namespace without having to specify a namespace. The using directive does not give you access to any namespaces that are nested within the specified namespace.

Namespaces are grouped into two categories: User-defined namespaces and system-defined namespaces. A user-defined namespace is a namespace defined in code. To view a list of system-defined namespaces, see the. NET Framework Class Library Reference.

For an example of referencing a method in another assembly, see Creating and using a C # DLL.

Example 1

Description

The following example shows how to define and use a using alias for a namespace:

Code

              Using Myalias = mycompany.proj.nested;//Define A alias to represent a namespace.namespace mycompany.proj{public class my Class{public static void Donothing () {}}}
Example 2

Description

The following example shows how to define a using directive and a using alias for a class:

Code

cs_using_directive2.cs//using directive.using system;//using alias for a class.using Aliastomyclass = Namespace1.mycl Ass;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
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.