Namespaces in C#

Namespaces

As of .net 2.0 there are five kinds of types we have, they are classes, structures, interfaces, enumeration and delegates. In C# namespaces are used to logically arrange these types.

Okay now the question is how to declare a namespace? It is really easy as below…

The C# language provides a keyword namespace to create a user defined namespace. The general form of declaring a namespace is as follows.

namespace <namespace_name>
{
// Classes and/or structs and/or enums etc.
}

namespace AcMilan
{
class MyClass
{
}
}

Now it is important to keep in mind while we declare a namespace that it is not possible to use any access specifiers like private, public etc with a namespace declarations. The namespaces in C# are implicitly having public access and this is not modifiable.

The namespace elements can’t be explicitly declared as private or protected. The namespace allows only public and internal elements as it members. The default is internal. In C# it is not possible to compile the following code since the class inside the namespace is declared as private.

namespace AcMilan
{
private class MyClass
{
}
}

The namespaces in C# can be nested. That means one namespace can contain other namespaces also. The .NET framework already contains number of standard namespaces like System, System.Net, System.IO etc. In addition to these standard namespaces the user can define their own namespaces.

Standard Namespaces in .NET

The following are some of the standard namespaces in .NET framework.

  • System: Contain classes that implement basic functionalities like mathematical operations, data conversions etc.
  • System.IO: Contains classes used for file I/O operations.
  • System.Net: Contains class wrappers around underlying network protocols.
  • System.Collections: Contains classes that implement collections of objects such as lists, hashtable etc.
  • System.Data: Contains classes that make up ADO.NET data access architecture.
  • System,Drawing: Contains classes that implement GUI functionalities.
  • System.Threading: Contains classes that are used for multithreading programming.
  • System.Web: Classes that implement HTTP protocol to access web pages.
  • System.Xml: Classes that are used for processing XML data.

These are some of impoetant namespaces of .NET framework. Remember that the above list is a not complete one.