Thursday, February 10, 2011

Namespaces and Using Statements C#

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?

  • 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?

  • 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.

    Colin Mackay : Why is it good to put the Using statements inside the namespace declaration?
    Daniel Earwicker : 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.
    JL : Earwicker - THANK YOU VERY MUCH!!! this is why I was getting confused in the first place!
    JL : So if anyone can answer that question I would also like to know the answer...
    JL : And StyleCop recommends you put the using statements in the namespace, but yeah - its hardly going to make a differance surely?
    Ray : Not really an answer as to why it's a good practice, but more discussion on it: http://stackoverflow.com/questions/125319/should-usings-be-inside-or-outside-the-namespace
    Eric Lippert : Here's my article discussing some of the subtleties of "inside vs outside" http://blogs.msdn.com/ericlippert/archive/2007/06/25/inside-or-outside.aspx
    Nissan Fan : Here's the answer as to why it's a good practice: http://blogs.msdn.com/ericlippert/archive/2007/06/25/inside-or-outside.aspx Also, if you work at MS or use StyleCop it will push you to this format.
    Nissan Fan : Sorry meant to include the link above.
    From Nissan Fan
  • 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.

    JL : Good! This makes sense then. Happy to know...
    Fredrik Mörk : How do you put the using statement outside the file?
    Brandon : Sorry, typo. Meant to say outside the namespace.
    From Brandon
  • 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.

  • 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.

    From tvanfosson
  • 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).

    From Daniel

0 comments:

Post a Comment