Search code examples
c#editorconfigc#-10.0

EditorConfig control File-scoped namespace declaration


I'm using C# 10 new feature File-scoped namespace declaration.

I have old code like this

namespace SampleCode
{
    public class MyClass
    {
    }
}

I'm moving this code to

namespace SampleCode;

public class MyClass
{
}

But I have a bunch of warnings : IDE0160: Convert to block scoped namespace

How do I make sure people will have warnings only with old syntax ?


Solution

  • To control the code style in editorconfig use this line :

    To enforce this style

    namespace SampleCode
    {
        public class MyClass
        {
        }
    }
    

    Add this line in .editorconfig

    # IDE0160: Convert to block-scoped namespace
    csharp_style_namespace_declarations = block_scoped:warning
    

    To enforce this style

    namespace SampleCode;
    
    public class MyClass
    {
    }
    

    Add this line in .editorconfig

    # IDE0161: Convert to file-scoped namespace
    csharp_style_namespace_declarations = file_scoped:warning