Working with Namespaces in C#

In C#, namespaces are used to logically arrange classes, structs, interfaces, enums and delegates. 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. 

Declaring a Namespace 

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.
}

Where namespace is the required keyword. The name of namespace can be any valid C# identifier or combinations of identifiers separated by commas.

For example:

using System;
namespace Rajesh.Csharp.Codes
{
class MyClass
{
public MyClass()
{
Console.WriteLine("My Class");
}
}
class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();// Displays 'My Class'
}
}
}

It is not possible to use any access specifiers like private, public etc with a namespace declarations. The namespaces in C# implicitly have 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.

The following code doesn't compile in C#, since the class inside the namespace is declared as private.

namespace Rajesh.Csharp.Codes
{
private class MyClass
{
}
}

Accessing Namespace Members 

The namespace members can be accessed by using a fully qualified name, which including the namespace name and member name separated by dot(.) from outside the namespace. For example 

using System;
namespace Rajesh.Csharp.Codes
{
class MyClass
{
public MyClass()
{
Console.WriteLine("My Class");
}
}
}
class MyClient
{
public static void Main()
{
//Using the fully qualified name to access the namespace member.
Rajesh.Csharp.Codes.MyClass mc = new Rajesh.Csharp.Codes.MyClass();
}
}

But in order to save developer from typing fully qualified name every time, the C# provides another keyword using to define some aliases to namespaces. Then while writing our code, we just refer the classes with their class name only. During compile time, the compiler will map all the class names with aliases to reach at the fully qualified name of the class. Once the fully qualified name has been found, it is used to convert the code to IL code. Remember that in IL code all classes, interfaces, enums and delegates are referenced with their fully qualified name.

An example of using is shown below. 

using Rajesh.Csharp.Codes;
MyClass mc = new MyClass();

Remember that inside C# namespaces, it is possible to use the keyword using.

namespace MyNameSpace
{
using System;
class MyClass
{
}
}

Any code contained within namespace MyNameSpace has access to System namespace without fully qualifying the type names, but those types are not members of the MyNameSpace namespace. 

The same namespace can be span over multiple lines as follows. 

using System;
namespace Rajesh.Csharp.Codes
{
class MyClass
{
public MyClass()
{
Console.WriteLine("My Class");
}
}
} 
namespace Rajesh.Csharp.Codes
{
class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();
}
}
}

Nesting Namespaces 

In C#, namespaces can be nested with each other as showing below.

using System;
namespace Outer
{
namespace Inner
{
class MyClass
{
public MyClass()
{
Console.WriteLine("My Class");
}
}
}
}
class MyClient
{
public static void Main()
{
Outer.Inner.MyClass mc = new Outer.Inner.MyClass();
}
}

Creating Aliases 

With the help of the keyword using, it is possible to create an alias name for a namespace or type. For example 

using con = System.Console; // Create an alias
class MyClient
{
public static void Main()
{
con.WriteLine("Hey rajesh! how you");
}
}

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 important namespaces of .NET framework. Remember that the above list is a not complete one.


Recommended Free Ebook
Similar Articles