Skip to content

Commit af5cbbd

Browse files
author
Andrew Hall
authored
Add documentation for ide0130 (#32885)
1 parent c8e8d91 commit af5cbbd

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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)

docs/fundamentals/code-analysis/style-rules/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ The following table list all the code-style rules by ID and [options](../code-st
119119
> | [IDE0090](ide0090.md) | Simplify `new` expression | [csharp_style_implicit_object_creation_when_type_is_apparent](ide0090.md#csharp_style_implicit_object_creation_when_type_is_apparent) |
120120
> | [IDE0100](ide0100.md) | Remove unnecessary equality operator | |
121121
> | [IDE0110](ide0110.md) | Remove unnecessary discard | |
122+
> | [IDE0130](ide0130.md) | Namespace does not match folder structure | [dotnet_style_namespace_match_folder](ide0130.md#dotnet_style_namespace_match_folder) |
122123
> | [IDE0140](ide0140.md) | Simplify object creation | [visual_basic_style_prefer_simplified_object_creation](ide0140.md#visual_basic_style_prefer_simplified_object_creation) |
123124
> | [IDE0150](ide0150.md) | Prefer `null` check over type check | [csharp_style_prefer_null_check_over_type_check](ide0150.md#csharp_style_prefer_null_check_over_type_check) |
124125
> | [IDE0160](ide0160-ide0161.md) | Use block-scoped namespace | [csharp_style_namespace_declarations](ide0160-ide0161.md#csharp_style_namespace_declarations) |

docs/fundamentals/code-analysis/style-rules/language-rules.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ The style rules in this section are applicable to both C# and Visual Basic.
7676
- [Use null propagation (IDE0031)](ide0031.md)
7777
- [Use 'is null' check (IDE0041)](ide0041.md)
7878
- [File header preferences](ide0073.md)
79+
- [Namespace naming preferences](ide0130.md)
7980

8081
## C# style rules
8182

docs/fundamentals/toc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,6 +1678,10 @@ items:
16781678
items:
16791679
- name: IDE0073
16801680
href: code-analysis/style-rules/ide0073.md
1681+
- name: Namespace naming preferences
1682+
items:
1683+
- name: IDE0130
1684+
href: code-analysis/style-rules/ide0130.md
16811685
- name: Unnecessary code rules
16821686
items:
16831687
- name: Overview

0 commit comments

Comments
 (0)