2

Whenever, I start a new project in C#, I get the following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

Why does using System; not allow you to use the sub namespaces with just the one line? I'm looking for an underlying reason as to why this isn't allowed. In the example given in the answer by HuorSwords:

namespace First {
    class A { }
}

namespace First.Second {
    class A { }
}

I would still get an error if I do:

using First;
using First.Second;

function void Test()
{
     A variable;
}

I would still have to differeniate between the two. So then why am I forced to declare both namespaces instead of just the one? Aside from potential ambiguity, is there any other reason why we have to declare each namespace like this?

PiousVenom
  • 6,888
  • 11
  • 47
  • 86
  • I've edited my question to point out I'm looking for an answer other than potential ambiguity, since that is something that is easily worked around. – PiousVenom Mar 27 '15 at 14:14
  • The answer is "that's the way it's designed". And don't dismiss potential ambiguity so easily - it's a _very_ significant reason. If `using System` imported _every_ namespace that existed in System, you'd have a much higher number of collisions. – D Stanley Mar 27 '15 at 14:22
  • @DStanley: I guess I'm looking for a "why it was designed that way" type of answer. I'm not dismissing the ambiguity answer. I understand it. But as per the example given, I'd still get an ambiguity error. So, I'm thinking there has to be another reason. – PiousVenom Mar 27 '15 at 14:27
  • 1
    But in that case your ambiguity is _explicitly_ created. Why should we be forced to deal with name collisions in sub-namespaces that we don't explicitly use? It's a trade-off - one which I've never had an issue with. – D Stanley Mar 27 '15 at 14:28

1 Answers1

1

Think about this situation:

namespace First {
    class A { }
}

namespace First.Second {
    class A { }
}

If the behavior could be as you propose, then when you reference First namespace declaring a using First; sentence, and declares a variable of class A, how the compiler can deduce what of your two A classes should be used?

You can reference any type without using any using sentence, just putting the full namespace reference when you declare the variable.

var firstA = new First.A();
var secondA = new First.Second.A();

Then, when you try to import two or more namespaces that contains classes equally named, the compiler raises an error in order you can specify what class should be used.

HuorSwords
  • 2,225
  • 1
  • 21
  • 34
  • Well, how would the compiler know the difference if I put a `using` for both? – PiousVenom Mar 27 '15 at 13:49
  • 1
    It would complain if you tried to use A without prefixing its namespace... naturally as it wouldn't know which A to instantiate. – Dave Lawrence Mar 27 '15 at 13:51
  • It doesn't know. The compiler show you an error indicating that exists a duplicated named class on your code and force you to use its namespace explicitly. – HuorSwords Mar 27 '15 at 13:51
  • But then that's no different than if I only did `using First;` – PiousVenom Mar 27 '15 at 14:00
  • 1
    @MyCodeSucks Except in that case you've _explicitly_ referenced namespaces that have collisions. If another assembly was created and referenced that added `First.Second` but your code only uses (and uses classes from) `First`, then your code would break _even if you don't use anything in the `First.Second` namespace_. – D Stanley Mar 27 '15 at 14:24