Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
20
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 10 NAMESPACES AND ASSEMBLIES

Namespace Names

As you saw, the name of a namespace can contain the name of the company that created the assembly. Besides identifying the company, the name is also used to help programmers get a quick idea of the kinds of types defined in the namespace.

Some important points about the names of namespaces are the following:

A namespace name can be any valid identifier, as described in Chapter 2.

Additionally, a namespace name can include any number of period characters. You can use this to organize types into hierarchies.

For example, Table 10-1 gives the names of some of the namespaces in the .NET BCL.

Table 10-1. Sample Namespaces from the BCL

 

System

 

System.IO

 

 

System.Data

 

Microsoft.CSharp

 

 

System.Drawing

 

Microsoft.VisualBasic

 

 

 

 

 

 

Namespace naming guidelines suggest the following:

Start namespace names with the company name.

Follow the company name with the technology name.

Do not name a namespace with the same name as a class or type.

For example, the software development department of the Acme Widget Company develops software in the following three namespaces, as shown in the following code:

AcmeWidgets.SuperWidget

AcmeWidgets.Media

AcmeWidgets.Games

namespace AcmeWidgets.SuperWidget

{

class SPDBase ...

...

}

279

CHAPTER 10 NAMESPACES AND ASSEMBLIES

More About Namespaces

There are several other important points you should know about namespaces:

Every type name in a namespace must be different from all the others.

The types in a namespace are called members of the namespace.

A source file can contain any number of namespace declarations, either sequentially or nested.

Figure 10-8 shows a source file on the left that declares two namespaces sequentially, with several types in each one. Notice that even though the namespaces contain several class names in common, they are differentiated by their namespace names, as shown in the assembly at the right of the figure.

Figure 10-8. Multiple namespaces in a source file

The .NET Framework BCL offers thousands of defined classes and types to choose from in building your programs. To help organize this vast array of available functionality, types with related functionality are declared in the same namespace. The BCL uses more than 100 namespaces to organize its types.

280

CHAPTER 10 NAMESPACES AND ASSEMBLIES

Namespaces Spread Across Files

A namespace is not closed. This means you can add more type declarations to it by declaring it again either later in the source file or in another source file.

For example, Figure 10-9 shows the declaration of three classes, all in the same namespace but declared in separate source files. The source files can be compiled into a single assembly, as shown in Figure 10-9, or into separate assemblies, as shown in Figure 10-10.

Figure 10-9. A namespace can be spread across source files and compiled to a single assembly.

Figure 10-10. A namespace can be spread across source files and compiled to separate assemblies.

281

CHAPTER 10 NAMESPACES AND ASSEMBLIES

Nesting Namespaces

Namespaces can be nested, producing a nested namespace. Nesting namespaces allows you to create a conceptual hierarchy of types.

There are two ways you can declare a nested namespace:

Textual nesting: You can create a nested namespace by placing its declaration inside the declaration body of the enclosing namespace. This is illustrated on the left in Figure 10-11. In this example, namespace OtherNs is nested in namespace MyNamespace.

Separate declaration: You can also create a separate declaration for the nested namespace, but you must use its fully qualified name in the declaration. This is illustrated on the right in Figure 10-11. Notice that in the declaration of nested namespace OtherNs, the fully qualified name

MyNamespace.OtherNS is used.

Figure 10-11. The two forms of declaring a nested namespace are equivalent.

Both forms of the nested namespace declarations shown in Figure 10-11 produce the same assembly, as illustrated in Figure 10-12. The figure shows the two classes declared in file SomeLib.cs, with their fully qualified names.

Figure 10-12. Nested namespace structure

Although the nested namespace is inside the enclosing namespace, its members are not members of the enclosing namespace. A common misconception is that since the nested namespace is inside the enclosing namespace, the members of the nested namespace must be a subset of the enclosing namespace. This is not true; the namespaces are separate.

282

k

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]