Thread: namespace query
View Single Post
  #2 (permalink)  
Old August 9th, 2016, 01:06 PM
Rod Stephens's Avatar
Rod Stephens Rod Stephens is offline
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts

First, no a using statement doesn't bring in sub-namespaces so "using System" does not include "System.Drawing" or any other System sub-namespace.

The purpose of namespaces is to not clutter the program up with symbols. For example, suppose System.Drawing and System.Collections.Generic both define some class. Then you wouldn't want them both included unless you really want to use both. Having them both lying around would make using them confusing. (It would also make compiling the program slower, although it would still run as fast because unused symbols are removed during compilation.)

Second, regarding "using static System.Console." Using "static" with "using" is a fairly new and weird. It lets you use the static methods of a class without naming the class. In this example, it means you can use the Console class's static methods such as Write and WriteLine without naming the class as in:

If you omit the "using" statement, then you need to write this:

So the "using static System.Console" directive doesn't include a namespace, it makes this special arrangement for the class.

Personally I think that's confusing because the code doesn't show what class owns the WriteLine method. Just looking at the code you would think it was in the current class (probably the form). I think Microsoft added this because it works in other languages rather than because it's something people wanted.

I assume with the other code that you're trying to show the structure of the System namespace (as opposed to trying to create your own namespaces with those names).

In that case, Console is a class not a namespace, so the system structure would look more like this:

namespace System
    public static class Console
    namespace Collections
        namespace Generic
But again including System does not include its sub-namespaces. It does include Console so your code can use Console.WriteLine, but you couldn't use classes in the sub-namespaces without including them, too.

One more note about using directives. If you look at them, Visual Studio grays out the ones that you aren't actually using. For example, if you include System.Collections but don't use any classes or other items defined in it, then it's grayed out.

You can remove them by right-clicking, picking Organize Usings, and then picking, Remove Unnecessary Usings.

That removes some clutter and, as I mentioned above, will speed up compiling a little, but won't change execution. (I usually just leave the unused ones that Visual Studio adds when it starts the project.)

I hope that helps. If it's still unclear, let me know.

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote