Namespaces and Using Directives
Asked Answered
S

6

7

If I have a namespace like:

namespace MyApp.Providers
 {
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Globalization;
  }

Does this mean that if I create other files and classes with the same namespace, the using statements are shared, and I don't need to include them again?

If yes, isn't this a bit of a management headache?

Sushi answered 20/7, 2009 at 22:1 Comment(2)
More details here: #125819Mogul
It's a directive, not a statement. The using Statement is something else entirely.Mong
I
12

No, it's only good for the namespace section inside the file. Not for all files inside the namespace.

If you put the using statement outside the namespace, then it applies to the entire file regardless of namespace.

It will also search the Usings inside the namespace first, before going to the outer scope.

Improvisation answered 20/7, 2009 at 22:4 Comment(2)
How do you put the using statement outside the file?Salmonella
Sorry, typo. Meant to say outside the namespace.Improvisation
T
2

You need to specify the using directive for any classes that you want to reference without qualification in each file where you want to use them.

Reference:

The scope of a using directive is limited to the file in which it appears.

Tigon answered 20/7, 2009 at 22:5 Comment(0)
L
0

No, it doesn't, and so no, it isn't.

Consider the fact that outside the namespace declaration, you are in the global namespace. Do using statements in that region of the source file affect the global namespace in other source files?

Lastly answered 20/7, 2009 at 22:3 Comment(0)
C
0

No. You'll need to include the namespaces in every class except on partial classes.

One side note: you're doing a very good practice of putting the using statements inside the Namespace. That's very good syntax.

Keep up the good work.

Corroboree answered 20/7, 2009 at 22:4 Comment(8)
Why is it good to put the Using statements inside the namespace declaration?Depilatory
Curious as to why you think its very good practice to do that? If you follow the good practice of putting each class in a separate file, it makes little difference whether you put using statements at the top of the file or the top of a namespace block.Lastly
Earwicker - THANK YOU VERY MUCH!!! this is why I was getting confused in the first place!Sushi
And StyleCop recommends you put the using statements in the namespace, but yeah - its hardly going to make a differance surely?Sushi
Not really an answer as to why it's a good practice, but more discussion on it: #125819Penicillate
Here's my article discussing some of the subtleties of "inside vs outside" blogs.msdn.com/ericlippert/archive/2007/06/25/…Backler
Here's the answer as to why it's a good practice: blogs.msdn.com/ericlippert/archive/2007/06/25/… Also, if you work at MS or use StyleCop it will push you to this format.Corroboree
Sorry meant to include the link above.Corroboree
S
0

The using statements are valid for the code file in which they appear, with a minor twist; if you put the using statements inside the namespace, they are limited to the scope of that namespace, but still only within the same code file.

Simplicity answered 20/7, 2009 at 22:5 Comment(0)
M
0

Usings only apply to the current file. Whether they're inside or outside the namespace declaration makes only a small difference:

The lookup order for types is as follows:

  1. start in the innermost namespace declaration
  2. look in the current namespace
  3. look in the usings of the current namespace
  4. go up to the parent namespace declaration and repeat from step 2

As a result, this program will compile fine:

namespace MyProject.Main {
    using System;

    class Program {
        public static void Main(string[] args) {
            Console.WriteLine("Hello, World!");
        }
    }
}

// in another file:
namespace MyProject.Console {
    class Test {}
}

But if you move the using System; to the top, then the compilation will fail (MyProject.Console.WriteLine doesn't exist).

Manzanares answered 20/7, 2009 at 23:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.