Namespace aliases in C#
Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
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.
Specifying correct namespace
We have to specify the namespace where SomeClass is defined to make this example run.
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.