22

Possible Duplicate:
Should Usings be inside or outside the namespace

sa1200 All using directives must be placed inside the namespace (StyleCop)

Is this just for code readibility or is there any actual advantage to doing so? Does it help the GC somehow?

Community
  • 1
  • 1
Matt
  • 25,943
  • 66
  • 198
  • 303
  • 4
    (Almost?) all the StyleCop rules are purely cosmetic... that's what it is for. FxCop is the one that makes functional suggestions. – jerryjvl Jul 01 '09 at 23:31

3 Answers3

13

It definitely won't help with GC.

Here's the discussion about two styles:

https://learn.microsoft.com/en-us/archive/blogs/abhinaba/stylistic-differences-in-using

https://learn.microsoft.com/en-us/archive/blogs/abhinaba/do-namespace-using-directives-affect-assembly-loading

Ishmaeel
  • 14,138
  • 9
  • 71
  • 83
SolutionYogi
  • 31,807
  • 12
  • 70
  • 78
4

If you have multiple namespaces in your project, you can limit which namespaces are used by each one individually.

This might come in handy if there were class names in two different namespaces that were the same. One might be the default in one part of your project, while the other could be the default in another.

Yes they look for some really fringe cases for these rules.

Brad Bruce
  • 7,638
  • 3
  • 39
  • 60
  • 6
    But you wouldn't have multiple namespaces in one file, would you? Because that would violate some other rule that I can't currently track down... – Mark Aug 24 '09 at 15:55
  • 2
    SA1403: File May Only Contain Single Namespace, Validates that a C# document does not contain more than one namespace directive. – string.Empty Aug 01 '13 at 12:40
  • These kind of type collisions were more common than I would have thought,. and not just with System.* either. Common type names like "Document" or "ILogger" or even something like "Invoice" coming from different namespaces but used in the same program due to a reference and local type or two referenced types can happen easily. – StingyJack Feb 15 '20 at 04:43
1

There is no runtime difference. It's purely a compile time (and development experience) change. The file, compiled IL will be identical in either case.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 4
    Actually, there are rare cases where making that move can introduce a change in the generated code. http://blogs.msdn.com/ericlippert/archive/2007/06/25/inside-or-outside.aspx – Eric Lippert Jul 02 '09 at 00:22
  • Eric: Doesn't VS2008 catch that? I thought that now provided a compile-time warning/error. I know this was a problem in 2005...but I thought it was now caught. – Reed Copsey Jul 02 '09 at 00:30
  • 4
    You move a line of code from one place to another, how does the compiler know that the old way was "right" and the new way was "wrong"? – Eric Lippert Jul 02 '09 at 02:18
  • I thought it gave a warning that there was a possible ambiguity. Guess not. – Reed Copsey Jul 02 '09 at 16:08