Program | specification | namespaces
In addition to relying on classes provided by several systems (such as System.Console), the procedures introduced so far are independent. But the more common scenario is that the actual application consists of a number of different parts, each of which is compiled separately. For example, an enterprise application might rely on several different components, including some internally developed components and some components purchased from independent software vendors.
Namespaces and assemblies help to develop component-based systems. namespaces provide a logical organizational system. Namespaces are used both as an "internal" organizational system for a program and as an "external" organizational system (a means of exposing program elements to other programs).
Assemblies are used for physical packaging and deployment. Assemblies can contain types, executable code to implement these types, and references to other assemblies.
There are two main assemblies: Applications and libraries. The application has a primary entry point, usually with an. exe file name extension, and the library has no main entry point, usually with a. dll file name extension.
To illustrate the use of namespaces and assemblies, this section again takes the "Hello, World" program described earlier as an example and divides it into two parts: a class library that provides messages and a console application that displays messages.
This class library contains only one class named HelloMessage. Example
HelloLibrary.cs
Namespace Microsoft.CSharp.Introduction
{
public class HelloMessage
{
public string Message {
get {
Return to "Hello, world";
}
}
}
}
Displays the HelloMessage class in the namespace named Microsoft.CSharp.Introduction. The HelloMessage class provides a read-only property named message. Namespaces can be nested, and declarations
Namespace Microsoft.CSharp.Introduction
{...}
A shorthand form that is nested only in several layers of namespaces. If it is not simplified, it should be declared as follows:
Namespace Microsoft
{
Namespace CSharp
{
Namespace Introduction
{...}
}
}
The next step in the "Hello, World" component is to write a console application that uses the HelloMessage class. You can use the fully qualified name of this class Microsoft.CSharp.Introduction.HelloMessage, but the name is too long to be used easily. A more convenient approach is to use a using namespace directive so that you do not have to qualify names when you use all the types in the corresponding namespaces. Example
HelloApp.cs
Using Microsoft.CSharp.Introduction;
Class HelloApp
{
static void Main () {
HelloMessage m = new HelloMessage ();
System.Console.WriteLine (M.message);
}
}
Displays a using namespace directive that references the Microsoft.CSharp.Introduction namespace. In this way, HelloMessage becomes the shorthand form of Microsoft.CSharp.Introduction.HelloMessage.
C # also allows you to define and use aliases. A using-alias-directive defines an alias for a type. This type of alias is useful when a name conflict occurs between two class libraries, or when a few types in a much larger namespace are used. Example
Using Messagesource = Microsoft.CSharp.Introduction.HelloMessage;
Shows a using-alias-directive that defines Messagesource as an alias for the HelloMessage class.
The code we have written can be compiled into a class library containing class HelloMessage and an application containing class HelloApp. The details of this compilation step may vary depending on the compiler or tool being used. When you use the command-line compiler provided in Visual Studio. NET, use the commands listed below:
Csc/target:library HelloLibrary.cs
Csc/reference:hellolibrary.dll HelloApp.cs
It produces a class library called HelloLibrary.dll and an application called HelloApp.exe.