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;
1
  • Just my opinion... but I absolutely hate it when people do this in our code base... uuggghhh. Commented Aug 13, 2013 at 11:09

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I know that this doesn't compile, I just stated the example to clarify what was on my mind. My question was if there's any way or a workaround for this issue.
@kobac I don't think so. If compiler tells you you shouldn't do this, you should not :)
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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.