1

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?

lonix
  • 14,255
  • 23
  • 85
  • 176

2 Answers2

4

Add to your .editorconfig:

csharp_using_directive_placement = inside_namespace

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

Jan Joneš
  • 777
  • 6
  • 13
3

An image says it all:

enter image description here

As you can see you can select which mode you want in Visual Studio (so it doesn't matter!).

'Options', 'Text Editor',' 'C#', 'Code Style'.

Poul Bak
  • 10,450
  • 5
  • 32
  • 57
  • Thanks for this Poul, upvoted! I'm sure many would benefit. I'm not using VS though. (linux, cli, vscode.) – lonix Oct 14 '22 at 03:42