'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 1:[1]

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

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

Solution 2:[2]

Update 2022-01-27

JetBrains Rider does support the dotnet_diagnostic.IDE* syntax starting from version 2021.3.2. This simplifies the setup into

EditorConfig

csharp_style_namespace_declarations = file_scoped
dotnet_diagnostic.IDE0161.severity = error

CSProj

<PropertyGroup>
  <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>

This will cover all the scenarios. Original answer below. Still worth to read it.


There are several different settings which you should control based on your desired state, used IDEs and workflow.

They are described in this article which I strongly recommend to read before you start building .editorconfig for your project.

Here is a summary for File-scoped, Block-scoped usings, respectively.

EditorConfig/CSproj setup for File-scoped usings


Visual Studio (error on violation)

EditorConfig

csharp_style_namespace_declarations = file_scoped
dotnet_diagnostic.IDE0161.severity = error

Note

Syntax option = rule:severity will be deprecated, sooner or later.


JetBrains Rider (error on violation)

EditorConfig

csharp_style_namespace_declarations = file_scoped:error

Note

Rider doesn't support dotnet_diagnostic.IDE* syntax.


CLI build e.g., CI/CD pipeline

EditorConfig

csharp_style_namespace_declarations = file_scoped
dotnet_diagnostic.IDE0161.severity = error

CSProj

<PropertyGroup>
  <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>

Recommended setup

EditorConfig

csharp_style_namespace_declarations = file_scoped:error
dotnet_diagnostic.IDE0161.severity = error

CSProj

<PropertyGroup>
  <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>

Note

Is the current .NET EditorConfig syntax a mess? Definitely.

EditorConfig/CSproj setup for Block-scoped usings


Visual Studio (error on violation)

EditorConfig

csharp_style_namespace_declarations = block_scoped
dotnet_diagnostic.IDE0160.severity = error

Note

Syntax option = rule:severity will be deprecated, sooner or later.


JetBrains Rider (error on violation)

EditorConfig

csharp_style_namespace_declarations = block_scoped:error

Note

Rider doesn't support dotnet_diagnostic.IDE* syntax.


CLI build e.g., CI/CD pipeline

EditorConfig

csharp_style_namespace_declarations = block_scoped
dotnet_diagnostic.IDE0160.severity = error

CSProj

<PropertyGroup>
  <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>

Recommended setup

EditorConfig

csharp_style_namespace_declarations = block_scoped:error
dotnet_diagnostic.IDE0160.severity = error

CSProj

<PropertyGroup>
  <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Brandon Minnick
Solution 2 Pang