dragan.stepanovic
dragan.stepanovic

Reputation: 3005

Using same alias for multiple namespaces

Is it possible to somehow use the same using alias for multiple namespaces, for which I know they don't have overlapping class names, in C#?
For example if I could do something like this:

using NSP = namespace1.namespace2;
using NSP = namespace1.namespace3;  

namespace2 and namespace3 don't have classes with the same name, so there're no worries about ambiguous class names, and it would be more convenient for me to write:

NSP.Class1 obj1 = new NSP.Class1();

than

NSP.namespace2.Class1 obj1 = new NSP.namespace2.Class1();

in case I use

using NSP = namespace1;

Upvotes: 3

Views: 1717

Answers (3)

Soner Gönül
Soner Gönül

Reputation: 98750

using NSP = namespace1.namespace2;
using NSP = namespace1.namespace3;  

You can't do that first of all. Compiler doesn't let you define same aliases for two different namespaces. That gives compiler time error.

namespace2 and namespace3 don't have classes with the same name, so there're no worries about ambiguous class names,

It doesn't matter they have same named classes or not, compiler doesn't let you do that.

Upvotes: 2

Damien
Damien

Reputation: 8987

No you can't, even the two namespaces don't have classes with the same name. You will get thsi error :

The using alias 'NSP' appeared previously in this namespace.

Upvotes: 0

Kurubaran
Kurubaran

Reputation: 8902

You can't use same alias name again. you will get the following compiler error

The using alias 'xxx' appeared previously in this namespace

Upvotes: 0

Related Questions