|
| 1 | +--- |
| 2 | +title: "IDE0130: Namespace does not match folder structure" |
| 3 | +description: "Learn about code analysis rule IDE0130: Namespace does not match folder structure" |
| 4 | +ms.date: 12/05/2022 |
| 5 | +ms.topic: reference |
| 6 | +f1_keywords: |
| 7 | +- IDE0130 |
| 8 | +- dotnet_style_namespace_match_folder |
| 9 | +helpviewer_keywords: |
| 10 | +- IDE0130 |
| 11 | +- dotnet_style_namespace_match_folder |
| 12 | +author: gewarren |
| 13 | +ms.author: gewarren |
| 14 | +dev_langs: |
| 15 | +- CSharp |
| 16 | +- VB |
| 17 | +--- |
| 18 | +# Namespace does not match folder structure (IDE0130) |
| 19 | + |
| 20 | +| Property | Value | |
| 21 | +| ------------------------ | ------------------------------------------------------ | |
| 22 | +| **Rule ID** | IDE0130 | |
| 23 | +| **Title** | Namespace does not match folder structure | |
| 24 | +| **Category** | Style | |
| 25 | +| **Subcategory** | Language rules (Namespace naming preferences) | |
| 26 | +| **Applicable languages** | C# and Visual Basic | |
| 27 | +| **Options** | `dotnet_style_namespace_match_folder` | |
| 28 | + |
| 29 | +## Overview |
| 30 | + |
| 31 | +This style rule uses the folder structure of the project to enforce namespace naming requirements. |
| 32 | + |
| 33 | +## Options |
| 34 | + |
| 35 | +Options specify the behavior that you want the rule to enforce. For information about configuring options, see [Option format](language-rules.md#option-format). |
| 36 | + |
| 37 | +### dotnet_style_namespace_match_folder |
| 38 | + |
| 39 | +| Property | Value | Description | |
| 40 | +| ------------------------ | ---------------------------------------------------- | -------------------------------------------------- | |
| 41 | +| **Option name** | dotnet_style_namespace_match_folder | | |
| 42 | +| **Option values** | `true` | Prefer namespace naming to match folder structure. | |
| 43 | +| | `false` | Disables the rule. | |
| 44 | +| **Default option value** | `true` | | |
| 45 | + |
| 46 | +> [!NOTE] |
| 47 | +> The `dotnet_style_namespace_match_folder` option depends on knowing the current project and root namespace properties. This information is provided by Visual Studio but is not available for command-line builds, such as `dotnet build`. For command-line builds to work, you must add the following properties to your *.editorconfig* file and replace the root directory and namespace placeholders with your values: |
| 48 | +> |
| 49 | +> ```ini |
| 50 | +> is_global=true |
| 51 | +> build_property.ProjectDir = <root directory> |
| 52 | +> build_property.RootNamespace = <root namespace> |
| 53 | +> ``` |
| 54 | +
|
| 55 | +## Example |
| 56 | +
|
| 57 | +Assume that the following code snippets are from a file named `Data/Example.cs` or `Data/Example.vb`, where `Data` represents the folder structure from the project file. The folder structure naming is added to the root namespace, which in this example is `Root`. |
| 58 | +
|
| 59 | +```csharp |
| 60 | +// Code with violations |
| 61 | +namespace Root.BadExample |
| 62 | +{ |
| 63 | + class Example |
| 64 | + { |
| 65 | + public void M() |
| 66 | + { |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | +
|
| 71 | +// Fixed code |
| 72 | +namespace Root.Data |
| 73 | +{ |
| 74 | + class Example |
| 75 | + { |
| 76 | + public void M() |
| 77 | + { |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
| 82 | +
|
| 83 | +```vb |
| 84 | +' Code with violations |
| 85 | +Namespace Root.BadExample |
| 86 | + Class Example |
| 87 | + Public Sub M() |
| 88 | + End Sub |
| 89 | + End Class |
| 90 | +End Namespace |
| 91 | + |
| 92 | +' Fixed code |
| 93 | +Namespace Root.Data |
| 94 | + Class Example |
| 95 | + Public Sub M() |
| 96 | + End Sub |
| 97 | + End Class |
| 98 | +End Namespace |
| 99 | +``` |
| 100 | + |
| 101 | +## Suppress a warning |
| 102 | + |
| 103 | +If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule. |
| 104 | + |
| 105 | +```csharp |
| 106 | +#pragma warning disable IDE0130 |
| 107 | +// The code that's violating the rule is on this line. |
| 108 | +#pragma warning restore IDE0130 |
| 109 | +``` |
| 110 | + |
| 111 | +To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md). |
| 112 | + |
| 113 | +```ini |
| 114 | +[*.{cs,vb}] |
| 115 | +dotnet_diagnostic.IDE0130.severity = none |
| 116 | +``` |
| 117 | + |
| 118 | +To disable all of the code-style rules, set the severity for the category `Style` to `none` in the [configuration file](../configuration-files.md). |
| 119 | + |
| 120 | +```ini |
| 121 | +[*.{cs,vb}] |
| 122 | +dotnet_analyzer_diagnostic.category-Style.severity = none |
| 123 | +``` |
| 124 | + |
| 125 | +For more information, see [How to suppress code analysis warnings](../suppress-warnings.md). |
| 126 | + |
| 127 | +## See also |
| 128 | + |
| 129 | +- [Language rules](language-rules.md) |
| 130 | +- [Code style rules reference](index.md) |
0 commit comments