Namespaces

Namespace

Namespaces make it easier to find identify types. Namespaces form a structure that helps to mitigate conflicts.

For example, the console class we have been using to write lines to the console is located within the System namespace. We can directly call the WriteLine function with System.Console.WriteLine(“xyz”);.

You can declare your own namespaces in the following manner.


namespace Outermost.Middle.Innermost{
{
	class class1{}
	class class2{}
}

//This is identical to
namespace Outermost
{
	namespace Middle
	{
		namespace Innermost
		{
			class class3{} //new classes
			class class4{}
		}
	}
}
                

Types that are not defined within a namespace are referred to as residing within global.

Namespace layers in use
Layers of namespaces

Using Directive

If you do not wish to use the fully qualified name of the type you are using, you can use the ‘using’ directive to import the namespace into your code.


using System;

public class Program
{
	public static void Main()
	{
		Console.WriteLine("Hello World");
	}
}
                

We do this in the case of Console. We import the System namespace so we do not have to enter System.Console every time we want to use the class.

The using directive should be treated with care, it is possible to import two or more namespaces that contain classes with the same identifier.

Using Static Directive

If you include the word static in your import, it is possible to not only import the namespace but also import the underlying static classes. This means you can use the member functions and properties of static classes without having to specify what class it belongs to. Using system and the Console class:


using static System.Console;

public class Program
{
	public static void Main()
	{
		WriteLine("Hello World");
	}
}
                

It is worth noting that this capability is only present in versions C# 6 and later.

Scope Within Namespaces

You can reference types defined in an outer namespace without explicit qualification in an inner namespace. For example:


                    namespace Outermost
{
	class Class1{}

	namespace Innermost
	{
		class Class2 : Class1 {}
		//Class 2 inherits from Class 1
	}
}
                

If the same two identifiers exist within the inner and outer namespaces, then the innermost namespace will be chosen when referencing the name directly.

For example, if Class1 existed in the inner and outermost namespaces, then calling Class1 in the inner would return the inner version of Class1.

Using Directives Within Other Namespaces

You can use the using directive inside an unrelated namespace without issue. For example:


                    namespace Outermost
{
	class Class1{}
}

namespace Innermost
{
	using Outermost;
	class Class2 : Class1 {}
	//Class 2 inherits from Class 1
}
                

Aliasing Namespace

If you are aware that two identifiers in different namespaces are going to collide, then you can import the types you need with an alias.

An alias will allow you to access the class, method, property etc that you need, but under an alternative name.


using System;
using SysConsole = System.Console;

public class Program
{
	public static void Main()
	{
		SysConsole.WriteLine("Hello World");
	}
}