[ACCEPTED]-What is the benefit of using namespace aliases in C#?-namespaces

Accepted answer
Score: 11

I use namespace aliases only if i have conflicts 3 with classes. for me it's not simplifing 2 at all. My opinion: if there is no need, don't 1 use it.

Score: 6

In cases where there are conflicting type 8 names, using an alias will save you from 7 having to use fully qualified names. The 6 name TextBox for example is used in both of the 5 following namespaces.

System.Windows.Forms.TextBox
System.Web.UI.WebControls.TextBox

Relying on the order 4 of the using declarations would be fairly 3 brittle and would only help with one of 2 the namespaces. Better would be to assign 1 each of the namespaces an alias.

Score: 6

As everyone else has said, it is useful 11 for disambiguating types when you have to 10 import multiple namespaces at the same time.

This 9 seems to be against other people's opinions, but 8 it may also be useful for clarifying scope 7 between two domains:

using Gilco.Test.ProjectX;
using DevCode = Gilco.Nilfum.Alpha.ProjectX;

public class MyTests
{
    public void TestCase1()
    {
        var car = new DevCode.Car();
        var testCar = new TestCar();
    }
}

It may also be useful 6 in the rare case when you have to specify 5 the namespace (for disambiguation), your 4 namespace isn't so long to justify the alias, but 3 is likely to change soon (like when you're 2 using namespaces to support two versions 1 of your code path simultaneously):

using DevCode = Gilco.V1;
Score: 5

They're useful when you have conflicts. For 11 example, if you have types NamespaceA.Jobber and NamespaceB.Jobber, and want 10 to use them both in the same class, you 9 won't be able to just add using statements for 8 NamespaceA and NamespaceB, because then 7 the compiler won't know what you're referring 6 to if you type Jobber. In this case you'd give 5 an alias to one or both of the namespaces.

This 4 can make your code clearer, especially if 3 the namespace is long, because the alternative 2 is to have the whole namespace written out 1 each time you use a type.

Score: 1

Namespace alias are useful for resolving 16 ambiguity of two or more class with the 15 same name in your code. For example you 14 have Button class from your winform and 13 you also have a Button class from your 3rd 12 library. When youur code refer to Button, you 11 may want to quantify it to from the 3rd 10 party and not wanting to put the full long 9 text everywhere and rather use the alias 8 using CompanyX = CompanyX.UI.Animated.Control ... CompanyX.Button

In fact I'm just using 7 it earlier in a work project to also make 6 my code more readable. I use Office Word 5 automation and instead of having variable 4 Application define everywhere and hard to 3 differentiate it from my actual Application 2 class, I define using Word=Microsoft.Office.Interop.Word and in the code I 1 can say Word.Application to refer to Word application object

Score: 0

I generally only use namespace aliases when 1 using two classes with the same name.

Score: 0

afaik when you create namespace alias - static 2 variables values for each alias is it's 1 own and not depended on other aliases + simplification

More Related questions