How to enforce file-scoped namespaces with StyleCop

38 Views Asked by At

I want this

namespace SampleCode
{
    public class MyClass
    {
    }
}

to become this

namespace SampleCode;

public class MyClass
{
}

with the help of StyleCop.

Is there any standard or custom rule for this?

1

There are 1 best solutions below

0
Artur On

In the .editorconfig file, add the following rule

csharp_style_namespace_declarations = file_scoped:error

Documentation: https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0160-ide0161