Learn C # (namespace) a week

zhaozj2021-02-16  54

Learn C # (namespace) a week

C # Talent Bird (QQ: 249178521)

Problem

The more the more the name, the more the names contain

The more names of W - the larger the likelihood, resulting in naming conflicts

W How do you name it to reflect the structure?

w Explicit prefix is ​​not a good solution

Sealed Class Book

{

...

}

Sealed Class Guibook

{

...

}

// Traditional naming method

2. Solve the method

· A named space is a logical naming system

w Namespace represents a range

W No .cs file can be inserted into any namespace in any namespace

W Alone. CS file can access multiple namespaces

Namespace GUI

{

Sealed Class Book

{

...

}

}

// Namespace solution

Methods using namespaces can reflect the logical relationships in the program. The above example shows that you declare a class name book in the GUI namespace instead of using the Guibook so long.

3. Nested namespace

· A namespace can contain other namespaces

w Use nested to reflect the structure of the program

w Namespace is always implicit to public

Namespace Rainforest

{

Namespace GUI

{

Sealed Class Book

{

...

}

}

}

Namespace rainforest.gui

{

Sealed Class Book

{

...

}

}

The above two programs are equivalent.

Namespaces 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 implied to public, that is, the declaration of the namespace cannot contain any access modifiers, and even public can not be added. The namespace is implied to public because any part of such programs can be accessed. It must also be noted that because the namespace is implied as public, it should be named using a PASCALCASE naming rule, that is, all words of all letters.

Nested namespaces can organize the logical structure of large programs very efficiently. But every layer repeats the keyword Namespace, it will be very difficult. But as shown in the example above, you can use a shorthand method.

4. Full name

· The namespace reflects the logical structure

W The full name of the point is lengthy and discussion

W But it is better than the name without some

Namespace rainforest.gui

{

Sealed Class Book

{

...

Private system.collections.hashtable pages;

}

}

The use of namespace can avoid naming conflicts, but it will result in an increase in the length of the name. For example, .NET Framework has a class called HashTable. This class is located in the collections namespace, and the Collectes namespace is also in the system namespace, which means that the full name of the HashTable class is System.Collections.hashtable (good).

5.using tag

· Using tag makes classes visible in naming spaces

W can only be used at the beginning of a namespace

Namespace rainforest.gui

{

Using system.collections;

...

Sealed Class Book

{

...

Private hashtable pages;

}

}

The USING logo can only be used at the beginning of the namespace, placed at the beginning of any class declaration statement. The role of the USING identifier is to use a skilled form to call classes in the namespace. For example, in the above example, the Book class declares a private field Pages, which is the HashTable class. The Book class declares that this field is using a short written form instead of its full name system.collections.hashtable. · Using tag can also be used in the .cs file

Using system; / / indicates global space

Sealed Class Global

{

Static void

Main

(String [] ARGS)

{

Console.write (Args [0]);

}

}

6. Quote alias

· USING alias produces an individual name for:

W class or namespace

W can only be used in the beginning of the namespace

Namespace rainforest.gui

{

Using hashtable = system.collections.hashtable;

Sealed Class Book

{

...

Hashtable pages;

}

}

转载请注明原文地址:https://www.9cbs.com/read-22160.html

New Post(0)