Introducing C# and the .NET Framework - Assemblies
(Page 11 of 12 )
Every time you compile C# code the file produced (.dll file or .exe file) is called an Assembly. The Assembly is a self-describing component; when you compile the source code files the compiler emits information about the types contained in the application, along with other information that is needed by the runtime (such as what files are needed to run the application). The Assembly is a logical group of one or more files, so you can have one assembly represented as MyAssembly.dll file, and you can have another assembly that consists of more than one file (or module).
The term "logical group of files" may be confusing for some of you. VS.NET generates a single-file assembly, which means that all the C# source code files will be compiled in one file with an extension .dll or .exe, which is the assembly of the application. The C# Compiler will emit information to tell the assembly that all the program types' definitions are grouped in this file, and that produces a single file assembly.
If you need to create a multi-file assembly, then you must use the C# Compiler to produce your program as modules and link these modules together using the C# compiler switches. Only one file in the assembly will contain information about the other parts (modules) of the assembly; that's why we call it assembly, because it is a group of one or more files linked together. You may want to produce a multi-file assembly if there are some parts of your program that will be rarely used, so it may be downloaded from the Internet only when needed. You will learn how to produce a multi-file assembly in an upcoming article (".NET Assemblies and Namespaces").
Namespaces
Suppose you have a class named Employee that represents an employee in the HR department. In the system there's another Employee class that represents an employee in the IT department. You want to define these classes, so the first thing that come to your mind is, how I can differentiate these separate entities? The answer is "use a namespace."
A namespace is just a logical organizing of your types to avoid name collision and to effectively organize your application's classes and other types. The .NET Framework uses namespaces to organize its class libraries. For example, the System namespace contains basic data types like Int32 and Int64. It also contains namespaces such as System.IO and System.Text. It's very easy to nest namespaces. Let's see the code for the Employee classes:
namespace HR
{
class Employee
{
// class functionality and code goes here
}
}
namespace IT
{
class Employee
{
// class functionality and code goes here
}
}
As you can see, the namespace HR contains a class called Employee which we can call using the form namespace.classname, HR.Employee. The IT namespace contains another class called Employee which we can refer to as IT.Employee. The next code can instantiate an object of each class with no problems:
// creating an instance of the HR.Employee class
HR.Employee emp = new HR.Employee();
// creating an instance of the IT.Employee class
IT.Employee emp2 = new IT.Employee();
A namespace declaration consists of the keyword namespace followed by an identifier for the namespace. You can nest namespaces as much as you want, but please avoid creating a long hierarchy of namespaces. Microsoft recommends a pattern for using namespaces:
CompanyName.ApplicationName.ComponentName
We can represent this pattern in the following namespace declarations:
namespace Microsoft
{
//classes that relate to this namespace goes here
class BasicFunctionality
{
}
//nested namespaces goes here
namespace Office
{
//classes that relate to this namespace goes here
class BasicFunctionality
{
}
//nested namespaces goes here
namespace Word
{
//Word classes goes here
}
}
}
We can create the same structure using a slightly different syntax (which is acceptable if you want to create these nested namespaces across multiple files):
namespace Microsoft
{
//classes that relate to this namespace goes here
class BasicFunctionality
{
}
}
namespace Microsoft.Office
{
//classes that relate to this namespace goes here
class BasicFunctionality
{
}
}
namespace Microsoft.Office.Word
{
//classes that relate to this namespace goes here
class BasicFunctionality
{
}
}
Now you can create each namespace in one C# source code file and they will be nested.
Next: Analyzing the Code >>
More C# Articles
More By Michael Youssef