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

CHAPTER 10 NAMESPACES AND ASSEMBLIES

Namespaces

In the MyWidgets example, since you have the source code, you can solve the name clash by just changing the name of the SquareWidget class in either the SuperLib source code or the UltraLib source code. But what if these libraries had been developed by separate companies, and you didn’t have the source code? Suppose that SuperLib was produced by a company called MyCorp, and UltraLib was produced by the ABCCorp company. In that case, you wouldn’t be able to use them together if you used any classes or types where there was a clash.

As you can imagine, with your development machine containing assemblies produced by dozens, if not hundreds, of different companies, there is likely to be a certain amount of duplication in the names of classes. It would be a shame if you couldn’t use two assemblies in the same program just because they happened to have type names in common.

Suppose, however, that MyCorp had a policy of prefacing all their classes with a string that consisted of the company name followed by the product name followed by the descriptive class name. Suppose further that ABCCorp had the same policy. In that case, the three class names in our example would be named MyCorpSuperLibSquareWidget, ABCCorpUltraLibSquareWidget, and

ABCCorpUltraLibCircleWidget, as shown in Figure 10-5. These are perfectly valid class names, and there’s little chance of the classes in one company’s library conflicting with those of another company.

Figure 10-5. With disambiguating strings prefaced to the class names, there is no conflict between the

libraries.

Our example program, however, would need to use these long names and would look like the following:

class WidgetsProgram

 

{

 

static void Main( )

 

{

 

MyCorpSuperLibSquareWidget sq

 

= new MyCorpSuperLibSquareWidget();

// From SuperLib

...

 

ABCCorpUltraLibCircleWidget circle

 

= new ABCCorpUltraLibCircleWidget();

// From UltraLib

...

 

}

 

}

 

275

CHAPTER 10 NAMESPACES AND ASSEMBLIES

Although this solves the conflict problem, these new, disambiguated names are harder to read and clumsy to work with, even with IntelliSense.

Suppose, however, that in addition to the characters normally allowed in an identifier, you could also use the period character within the string—although still not at the beginning or at the end of the class name. In this case, we could make the names more understandable, such as

MyCorp.SuperLib.SquareWidget, ABCCorp.UltraLib.SquareWidget, and ABCCorp.UltraLib.CircleWidget. Now the code would look like the following:

class WidgetsProgram

 

{

 

static void Main( )

 

{

 

MyCorp.SuperLib.SquareWidget sq

 

= new MyCorp.SuperLib.SquareWidget();

// From SuperLib

...

 

ABCCorp.UltraLib.CircleWidget circle

 

= new ABCCorp.UltraLib.CircleWidget();

// From UltraLib

...

 

}

 

}

 

This brings us to the concept of the namespace name and a namespace.

You can think of a namespace name as a string of characters (that can include periods inside the string) tacked on to the front of the class or type name and separated by a period.

The full string including the namespace name, separating period, and the class name is called the class’s fully qualified name.

A namespace is the set of classes and types that share that namespace name.

Figure 10-6 illustrates these definitions.

Figure 10-6. A namespace is the set of type definitions that share the same namespace name.

276

CHAPTER 10 NAMESPACES AND ASSEMBLIES

You can use namespaces to group a set of types together and give them a name. Generally, you want namespace names to be descriptive of the types contained by the namespace and to be distinct from other namespace names.

You create a namespace by declaring the namespace in the source file that contains your type declarations. The following shows the syntax for declaring a namespace. You then declare all your classes and other types between the curly braces of the namespace declaration. These are then the members of the namespace.

Keyword

 

Namespace name

 

 

namespace NamespaceName

{

TypeDeclarations

}

The following code shows how the programmers at MyCorp could create the MyCorp.SuperLib namespace and declare the SquareWidget class inside it.

Company name Period

namespace MyCorp.SuperLib

{

public class SquareWidget

{

public double SideLength = 0; public double Area

{

get { return SideLength * SideLength; }

}

}

}

277

CHAPTER 10 NAMESPACES AND ASSEMBLIES

Now, when the MyCorp company ships you the new updated assembly, you can use it by modifying your MyWidgets program, as shown here:

class WidgetsProgram

 

 

 

 

 

 

{

 

 

 

 

 

 

 

 

 

static void Main( )

 

 

 

 

 

 

{

 

Fully qualified name

 

 

 

Fully qualified name

 

 

 

 

 

 

 

 

MyCorp.SuperLib.SquareWidget sq = new MyCorp.SuperLib.SquareWidget();

 

 

 

 

 

 

 

Namespace name

Class name

CircleWidget circle = new CircleWidget();

...

Now that you have explicitly specified the SuperLib version of SquareWidget in your code, the compiler will no longer have a problem distinguishing the classes. The fully qualified name is a bit long to type, but at least you can now use both libraries. A little later in the chapter, we’ll cover the using alias directive to solve the inconvenience of having to repeatedly type in the fully qualified name.

If the UltraLib assembly is also updated with a namespace by the company that produces it (ABCCorp), then the compile process would be as shown in Figure 10-7.

Figure 10-7. Class libraries with namespaces

278

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