7

Possible Duplicate:
Should Usings be inside or outside the namespace

I'm supporting some code that, unusually, has all its using statements contained within the namespace declaration. It makes me a bit uncomfortable but I have no idea if this should be anything to be concerned about or not. I can't think of an immediate problem other than it being counter to usual convention.

Is there anything wrong with:

namespace myProject.controls
{
    using System;
    using System.Collections;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

Instead of the more widely-used:

using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace myProject.controls
{
Community
  • 1
  • 1
Phil.Wheeler
  • 16,748
  • 10
  • 99
  • 155

3 Answers3

2

There's nothing to be concerned with here; in fact, the first example (with the using statements inside the namespace declaration) is recommended by default by StyleCop.

Adam Maras
  • 26,269
  • 6
  • 65
  • 91
2

The only difference has to do with scoping. In the first case, the using declarations are scoped within the namespace declaration, and are not available outside it, in other namespace declarations in the same file, for example. In the second case, the declarations are in scope for the whole file.

Jordão
  • 55,340
  • 13
  • 112
  • 144
0

Location of the using statement affects the scope of the references within the file.

I cannot think of any situation that would 'warrant' limiting it withing a smaller scope. Though, I would prefer sticking to conventions - or commenting exhaustively - , just to make sure 'next person' does not have to guess my intentions.

Probably this link will answers your question in detail

Community
  • 1
  • 1
YetAnotherUser
  • 9,156
  • 3
  • 39
  • 53