Namespace aliases in C#

gpeipman
1,190 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Namespace aliases

Namespaces may have very long names. It specially holds true for very big libraries and systems. Sometimes we are forced to use namespace before type in our code. By example, if there are two classes with same name but in different namespace then we have to specify from which namespace we want to use the class if both namespaces are added to usings block of our class.

Using namespaces in class definition

Let's see the example. There is class called SomeClass in two different namespaces and the class is instantiated in Main() method. Run the demo and see what happens.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using SomeVery.LongLong.Namespace;
using OtherNamespace;
namespace SomeVery.LongLong.Namespace
{
public class SomeClass
{
public string GetName()
{
return "Someclass from long-long namespace";
}
}
}
namespace OtherNamespace
{
public class SomeClass
{
public string GetName()
{
return "Someclass from other namespace";
}
}
}
namespace Program
{
class Hello
{
static void Main()
{
var someClass = new SomeClass();
Console.WriteLine(someClass.GetName());
}
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Specifying correct namespace

We have to specify the namespace where SomeClass is defined to make this example run.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using SomeVery.LongLong.Namespace;
using OtherNamespace;
namespace SomeVery.LongLong.Namespace
{
public class SomeClass
{
public string GetName()
{
return "Someclass from long-long namespace";
}
}
}
namespace OtherNamespace
{
public class SomeClass
{
public string GetName()
{
return "Someclass from other namespace";
}
}
}
namespace Program
{
class Hello
{
static void Main()
{
var someClass = new SomeVery.LongLong.Namespace.SomeClass();
Console.WriteLine(someClass.GetName());
}
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Using namespace alias

The definition of someClass looks ugly, really ugly. I have seen some cases when namespace has very long name and class has long name too. So, things can be even worse than shown here. What we can do is to introduce namespace alias. Next code sample uses longNS as alias for very long namespace.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using longNS = SomeVery.LongLong.Namespace;
using OtherNamespace;
namespace SomeVery.LongLong.Namespace
{
public class SomeClass
{
public string GetName()
{
return "Someclass from long-long namespace";
}
}
}
namespace OtherNamespace
{
public class SomeClass
{
public string GetName()
{
return "Someclass from other namespace";
}
}
}
namespace Program
{
class Hello
{
static void Main()
{
var someClass = new longNS.SomeClass();
Console.WriteLine(someClass.GetName());
}
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content