Why don’t namespace using directives import nested namespaces?

In C#, when you specify a “using” clause, such as

using System.Text;

the compiler only imports the types in System.Text into the global namespace – it doesn’t do the same with any namespaces inside of System.Text. So, while that using allows me to write:

StringBuilder s = new StringBuilder();

it does not allow me to write:

RegularExpressions.Regex r = new RegularExpressions.Regex();


Well, an early version of the C# compiler had that behavior, but we found that it had a few issues:

First, it made the code harder to follow. In the above example, RegularExpressions.Regex() could be a global name, or a name based on any of my using clauses. Having to look at the top of your code to figure out what a name is is something we’d like to avoid.

The second problem had to do with collisions between namespace names, which occurred much more often with this behavior. If there was another namespace with a RegularExpressions namespace inside of it, the user wouldn’t be able to have a “using” statement for both – even if they didn’t actually care about that namespace.

We therefore decided to change the behavior.

Author: Eric Gunnerson]