Search code examples
c#roslync#-10.0

Usings before/after file-scoped namespace


C# 10 gave us file-scoped namespaces. So this:

using Foo;
using Bar;
namespace Baz {
  // ...
}

can be written as:

using Foo;
using Bar;
namespace Baz;
// ...

There are differences between usings before and after a namespace. Does the compiler automatically reorder that to:

namespace Baz;
using Foo;
using Bar;
// ...

If not, is there a dotnet build or msbuild CLI switch, code analyzer or some other option to enable such a thing?


Solution

  • Add to your .editorconfig:

    csharp_using_directive_placement = inside_namespace
    

    See https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0065