Reputation: 3005
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
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
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
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