using inside or outside of namespace?

mskeel

Senior Contributor
Joined
Oct 30, 2003
Messages
913
What is the difference between putting your using statements outside vs inside of a namespace? I guess the issue is this, when a class is created by visual studio it looks similiar to this:
C#:
using System;  //using system on the outside of the namespace

namespace super
{
   //examples always seem to put their using statements here...

   /// <summary>
   /// put summary here
   /// </summary>
   public class duper
   {     
      public duper()
      {
         // TODO: put initialize code here
      }
   }
}
Looking through example code, though, shows the using statements on the inside of the namespace. What is the difference? Does the placement of the using statement scope the linking similiarly to C++ or does it not really matter? Advantages or disadvantages to either placement? What is the difference?
 
I don't think there is a difference. I know when using the CodeDOM to generate code, something like this:

Visual Basic:
CodeNamespace ns = new CodeNamespace();
ns.Name = "MyNameSpace";
ns.Imports.Add(new CodeNameSpaceImport("System"));
ns.Imports.Add(new CodeNameSpaceImport("System.Data"));

will put the using statements on the inside of the root namespace, like you indicated. But this has does not seem to affect anything. It works the same as if the using statements were declared before the namespace declaration. I have been using generated code with using statements positioned like this for close to 2 years now and have never had a problem with it.
 
It was added to Patterns and Practices since it directly relates the classes to the current namespace. (not that you'll often have multiple namespaces in the same file)
 
I suppose that is internaly consistant (compairing C# to VB.Net) becuase VB puts a root level namespace via the project properties for all classes in a project where C# requires the namespace be written in each class file. So, really the imports statements are inside a namespace.

Just so I understand completely, there is no impact on compiling or execution by putting using statements inside or outside of a namespace. It is considered best practice to put using statements inside the namespace.

I have found some coding standards docuemtns that say not to put using statements inside of namespaces. The linke at the top of this article. Alos, this coding standard says to always at the top of the page. Meanwhile all the examples put using statements inside, VS outside, and so far two reccomendations from this forum for inside.

So how's it supposed to go? Is there any actual reason for doing it one way over another? Readability/maintainabilty is the only thing I can think of right now. The most important thing is that there is no actual difference between inside and outside, right? It's just a style thing?
 
The difference is so minimal that it can be ignored. Usually I try to follow P&P to the T, but since VS puts it outside by default I usually just end up leaving it there.
 
I always thought that it was just a matter of convenience and completely a code writing style choice by the developer. This doesn't seem to reflect a design pattern. My CodeDom example earlier is inline with how a vb.net project has a root level namespace but a c# project may not. Is there really a design pattern that dictates where the using statements should go?
 
Back
Top