31

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 ?

JuChom
  • 5,717
  • 5
  • 45
  • 78

2 Answers2

35

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
Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
JuChom
  • 5,717
  • 5
  • 45
  • 78
  • 8
    Enforcing it in .editorconfig made the option to refactor the entire solution to file scoped namespaces avalible in VS2022 Preview, great! – Oskar Oct 22 '21 at 08:19
18

Update 2022-01-27 (all scenarios setup)

JetBrains Rider does support the dotnet_diagnostic.IDE* syntax starting from version 2021.3.2.
This simplifies the setup for all possible scenarios into this:

EditorConfig

csharp_style_namespace_declarations = file_scoped:error
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>
KUTlime
  • 5,889
  • 1
  • 18
  • 29