One week C # (namespace)

Source: Internet
Author: User

1. Problem

·A larger program contains more names.

More names-The higher the possibility of naming conflicts

How do you name to reflect the structure

Explicit prefixes are not a good solution.

//Traditional naming methods

Sealed class Book

{

}

Sealed class guibook

{

}

 

2. Solution

·A namespace is a logical naming system.

Namespace indicates a range

Any. CSYou can insert a class in any namespace in the file.

A separate. CSFiles can access multiple namespaces

//Namespace Solution

Namespace Gui

{

Sealed class Book

{

}

}

The namespace method can reflect the logical relationship in the program. The above example shows that you declare a class named book in the GUI namespace, instead of using a name as long as guibook.

3. nested namespaces

·A namespace can contain other namespaces.

Use nesting to reflect program structure

The namespace is always implicitlyPublic

Namespace rainforest

{

Namespace Gui

{

Sealed class Book

{

}

}

}

 

Namespace rainforest. Gui

{

Sealed class Book

{

}

}

 

The above two programs are equivalent.

A namespace can contain classes and other namespaces, but cannot contain data.

The structure between nested namespaces reflects the logical structure of the program organization.

The namespace is implicitly public. That is to say, the namespace declaration cannot contain any access modifier or even public. The namespace is implicitly public because any part of a program can access it. Note that because the namespace is implicitly public, pascalcase naming rules should be used for naming, that is, uppercase letters of all words.

Nested namespaces can effectively organize the logical structure of large programs. However, if the keyword namespace is repeatedly typed for each layer, it will be very complicated. However, as shown in the preceding example, you can use the abbreviated method.

4. Full name

·The namespace reflects the logical structure.

The full name of a vertex is lengthy and annoying.

But it is better than a name without a dot.

 

Namespace rainforest. Gui

{

Sealed class Book

{

Private system. Collections. hashtable pages;

}

}

Namespace can avoid naming conflicts, but it will increase the name length. For example,. NET Framework has a class named hashtable. This class is located in the collections namespace, And the collections namespace is located in the system namespace, which means that the full name of the hashtable class is system. Collections. hashtable (long ).

5. Using mark

·UsingMark to make the class visible in the namespace

It can only be used at the beginning of a namespace

Namespace rainforest. Gui

{

Using system. collections;

Sealed class Book

{

Private hashtable pages;

}

}

The Using ID can only start with the namespace and be placed at the beginning of any class declaration statement. The Using identifier is used to call classes in the namespace in short form. For example, in the above example, the book class declares a private field pages, which is of the hashtable class. The book class declares this field in short form, rather than its full name system. Collections. hashtable.

·UsingTags can also be used. CSFile start

Using system; // indicates the global space.

Sealed class global

{

Static void main (string [] ARGs)

{

Console. Write (ARGs [0]);

}

}

 

6. Alias reference

·Using Alias Generates an alias:

Class or namespace

It can only start with a namespace.

Namespace rainforest. Gui

{

Using hashtable = system. Collections. hashtable;

Sealed class Book

{

Hashtable pages;

}

}

 

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.