This example proves that modules in an assembly are loaded only as and when needed. Also, when deploying the application, the Util.dll assembly and the two modules must be in tandem. If any of the modules is missing during runtime, you will encounter a runtime error, as shown in Figure 15-20.

Figure 15-20

Understanding Namespaces and Assemblies

As you know from Chapter 1, the various class libraries in the .NET Framework are organized using namespaces. So how do namespaces relate to assemblies? To understand the relationship between namespaces and assemblies, it's best to take a look at an example.

Create a new Class Library project in Visual Studio 2008, and name it ClassLibrary1. In the default Class1.cs, populate it with the following:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Learn2develop.net {

 public class Class1 {

  public void DoSomething() {

  }

 }

}

Observe that the definition of Class1 is enclosed within the Learn2develop.net namespace. The class also contains the DoSomething() method.

Add a new class to the project by right-clicking on the project's name in Solution Explorer and selecting Add→Class (see Figure 15-21).

Figure 15-21

Use the default name of Class2.cs. In the newly added Class2.cs, code the following:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Learn2develop.net {

 public class Class2 {

  public void DoSomething() {

  }

 }

}

Class2 is enclosed within the same namespace — Learn2develop.net, and it also has a DoSomething() method. Compile the ClassLibrary1 project so that an assembly is generated in the binDebug folder of the project — ClassLibrary1.dll. Add another Class Library project to the current solution and name the project ClassLibrary2 (see Figure 15-22).

Figure 15-22 

Populate the default Class1.cs as follows:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Learn2develop.net {

 public class Class3 {

  public void DoSomething() {

  }

 }

}

namespace CoolLabs.net {

 public class Class5 {

  public void DoSomething() {

  }

 }

}

This file contains two namespaces — Learn2develop.net and CoolLabs.net — each containing a class and a method.

Compile the ClassLibrary2 project so that an assembly is generated in the binDebug folder of the project — ClassLibrary2.dll.

Now, add another Class Library project to the current solution, and this time use the Visual Basic language. Name the project ClassLibrary3 (see Figure 15-23).

Figure 15-23

In the Properties page of the ClassLibrary3 project, set its root namespace to Learn2develop.net (see Figure 15-24).

Figure 15-24

In the default Class1.vb, define Class4 and add a method to it:

Public Class Class4

 Public Sub DoSomething()

 End Sub

End Class

Compile the ClassLibrary3 project so that an assembly is generated in the binDebug folder of the project — ClassLibrary3.dll.

Now add a new Windows application project (name it WindowsApp) to the current solution so that you can use the three assemblies (ClassLibrary1.dll, ClassLibrary2.dll, and ClassLibrary3.dll) that you have created.

To use the three assemblies, you need to add a reference to all of them. Because the assemblies are created in the same solution as the current Windows project, you can find them in the Projects tab of the Add Reference dialog (see Figure 15-25).

Figure 15-25

In the code-behind of the default Form1, type the Learn2develop.net namespace, and IntelliSense will show that four classes are available (see Figure 15-26).

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату