Namespace declaration preferences (IDE0160 and IDE0161)

This article describes two related rules, IDE0160 and IDE0161.

Property Value
Rule ID IDE0160
Title Use block-scoped namespace
Category Style
Subcategory Language rules (code-block preferences)
Applicable languages C#
Options csharp_style_namespace_declarations
Property Value
Rule ID IDE0161
Title Use file-scoped namespace
Category Style
Subcategory Language rules (code-block preferences)
Applicable languages C#
Options csharp_style_namespace_declarations

Overview

These rules apply to namespace declarations. For IDE0161 to report violations when block-scoped namespaces are used, you must set the associated option to file_scoped.

Options

The option value specifies whether namespace declarations should be block scoped or file scoped. By default, namespace declarations are block-scoped. This option is used by Visual Studio to determine how namespaces are declared when new code files are added to projects. Visual Studio honors the option value even if both IDE0160 and IDE0161 are disabled.

For information about configuring options, see Option format.

csharp_style_namespace_declarations

Property Value Description
Option name csharp_style_namespace_declarations
Applicable languages C#
Introduced version Visual Studio 2019
Option values block_scoped Namespace declarations should be block scoped.
file_scoped Namespace declarations should be file scoped.
Default option value block_scoped
// csharp_style_namespace_declarations = block_scoped
using System;

namespace Convention
{
    class C
    {
    }
}

// csharp_style_namespace_declarations = file_scoped
using System;

namespace Convention;
class C
{
}

Suppress a warning

If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

#pragma warning disable IDE0160 // Or IDE0161
// The code that's violating the rule is on this line.
#pragma warning restore IDE0160 // Or IDE0161

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.IDE0160.severity = none
dotnet_diagnostic.IDE0161.severity = none

To disable all of the code-style rules, set the severity for the category Style to none in the configuration file.

[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Style.severity = none

For more information, see How to suppress code analysis warnings.

See also