7

Possible Duplicate:
Should Usings be inside or outside the namespace

Are there any technical reasons for preferring this

namespace Foo
{
     using System;
     using System.IO;

instead of the default

using System;
using System.IO;

namespace Foo
{
Community
  • 1
  • 1
ConfusedNoob
  • 9,826
  • 14
  • 64
  • 85
  • 1
    See http://stackoverflow.com/questions/125319/should-usings-be-inside-or-outside-the-namespace – chrisaut Sep 02 '11 at 20:24
  • It works both way but Generally People prefer it outside the namespace but you might look at this [Using inside Namespace or Outside](http://stackoverflow.com/questions/125319/should-usings-be-inside-or-outside-the-namespace) – Nivid Dholakia Sep 02 '11 at 20:24

3 Answers3

7

Eric Lippert explains this.

In general, they're identical.
However, using statements in the namespace can see namespaces and aliases included outside the namespace.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

Almost* the only difference between the two would be if you used more than one namespace in the same file (or if you used the same namespace more than once). I'm not sure why would you do that, bu you certainly can:

using System;

namespace FooNamespace
{
    using System.IO;

    class Foo
    {
        // you can use types from System and System.IO directly here
    }
}

namespace BarNamespace
{
    class Bar
    {
        // you can't use types from System.IO directly here
        // but you can use types from System
    }
}

* See SLaks' answer.

svick
  • 236,525
  • 50
  • 385
  • 514
  • This is useful for extension methods. http://blog.slaks.net/2011/07/creating-local-extension-methods.html – SLaks Sep 02 '11 at 20:33
0

No technical reason, just a preference. of course the second chunk of code looks cleaner, though.

Nexus
  • 968
  • 1
  • 12
  • 31