3

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;
dragan.stepanovic
  • 2,955
  • 8
  • 37
  • 66

3 Answers3

2
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.

Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0

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

The using alias 'xxx' appeared previously in this namespace

Kurubaran
  • 8,696
  • 5
  • 43
  • 65
0

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.

Damien
  • 8,889
  • 3
  • 32
  • 40