A Namespace in Microsoft .Net is like containers of objects. They may contain unions, classes, structures, interfaces, enumerators and delegates. Main goal of using namespace in .Net is for creating a hierarchical organization of program. In this case a developer does not need to worry about the naming conflicts of classes, functions, variables etc., inside a project.
In Microsoft .Net, every program is created with a default namespace. This default namespace is called as global namespace. But the program itself can declare any number of namespaces, each of them with a unique name. The advantage is that every namespace can contain any number of classes, functions, variables and also namespaces etc., whose names are unique only inside the namespace. The members with the same name can be created in some other namespace without any compiler complaints from Microsoft .Net.
To declare namespace C# .Net has a reserved keyword namespace. If a new project is created in Visual Studio .NET it automatically adds some global namespaces. These namespaces can be different in different projects. But each of them should be placed under the base namespace System. The names space must be added and used through the using operator, if used in a different project.
Please now have a look at the example of declaring some namespace:
using System; namespace OutNamespace { namespace WorkNamespace { /// can be placed some classes, structures etc. } }
In this example we create two namespaces. These namespaces have hierarchical structure. We have some outer one named OutNamespace and the inner one called WorkNamespace. The inner namespace is declared with a C# .Net class WorkItem.
The next logical discussion after a namespace is classes. A class is the basis of object oriented programming. It encapsulates the data and methods into one itself and manipulates them through the interaction with that object.
class WorkItem { public WorkItem() { } static WorkItem() { m_counter = 1; } public static int GetCounter() { return m_counter; } private static int m_counter; public virtual void Status() { } internal bool IsWorking { get { return m_isWorking; } set { m_isWorking = value; } } private bool m_isWorking = true; }
The above sample contains the .Net namespace with the class WorkItem inside it.
As already discussed, a class is the basis of Object oriented programming. A class must have a constructor. The constructor method is used for initialization purposes. Each class can have different modifiers which pertains to the type and functionality of the class. Some such modifiers are: new, public, protected, internal, private, abstract, and sealed. A class members can also have these modifiers. In the above example, there is declared special constructor type static constructor. It uses only for class not for its instances. In the same way we can access to the static members of class.
OutNamespace.WorkNamespace.WorkItem item = new WorkItem(); int i = WorkItem.GetCounter();
In this piece of code there was created some instance of WorkItem but called using its full name (including all namespace’s names). The second line it is an access to the public static property of WorkItem. There are many advanced features that can be used in class constructing process. One of them polymorphism that can be realized though the virtual methods.
Download the sample code from here. It can be compiled using csc command of Microsoft .NET command line. Just only type the following string: csc filename.cs. It creates file with name filename.exe that can be run as standard executable file.