Skip to content

Commit 90e749f

Browse files
authored
Add documentation for IDE0360, IDE0370, and IDE0380 code style rules (#49681)
1 parent 54464ad commit 90e749f

File tree

6 files changed

+268
-1
lines changed

6 files changed

+268
-1
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
title: "IDE0360: Simplify property accessor"
3+
description: "Learn about code analysis rule IDE0360: Simplify property accessor"
4+
ms.date: 11/08/2025
5+
f1_keywords:
6+
- IDE0360
7+
helpviewer_keywords:
8+
- IDE0360
9+
dev_langs:
10+
- CSharp
11+
ai-usage: ai-assisted
12+
---
13+
# Simplify property accessor (IDE0360)
14+
15+
| Property | Value |
16+
|--------------------------|-----------------------------------------------|
17+
| **Rule ID** | IDE0360 |
18+
| **Title** | Simplify property accessor |
19+
| **Category** | Style |
20+
| **Subcategory** | Language rules (expression-level preferences) |
21+
| **Applicable languages** | C# 13+ |
22+
| **Options** | `csharp_style_prefer_simple_property_accessors` |
23+
24+
## Overview
25+
26+
This rule flags places where a property accessor that directly accesses the `field` keyword (C# 13+) can be simplified. When a property accessor only returns `field` or assigns a value to `field`, it can be simplified to a simple auto-accessor.
27+
28+
## Options
29+
30+
Options specify the behavior that you want the rule to enforce. For information about configuring options, see [Option format](language-rules.md#option-format).
31+
32+
### csharp_style_prefer_simple_property_accessors
33+
34+
| Property | Value | Description |
35+
|-------------------|-----------------------------------------------|-------------------|
36+
| **Option name** | `csharp_style_prefer_simple_property_accessors` | |
37+
| **Option values** | `true` | Prefer simplified property accessors |
38+
| | `false` | Disables the rule |
39+
| **Default option value** | `true` | |
40+
41+
## Example
42+
43+
```csharp
44+
// Code with violations.
45+
public int Prop
46+
{
47+
get { return field; }
48+
set { field = (value > 0) ? value : throw new ArgumentException(); }
49+
}
50+
51+
// Fixed code.
52+
public int Prop { get; set; }
53+
```
54+
55+
## Suppress a warning
56+
57+
If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
58+
59+
```csharp
60+
#pragma warning disable IDE0360
61+
// The code that's violating the rule is on this line.
62+
#pragma warning restore IDE0360
63+
```
64+
65+
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md).
66+
67+
```ini
68+
[*.{cs,vb}]
69+
dotnet_diagnostic.IDE0360.severity = none
70+
```
71+
72+
To disable all of the code-style rules, set the severity for the category `Style` to `none` in the [configuration file](../configuration-files.md).
73+
74+
```ini
75+
[*.{cs,vb}]
76+
dotnet_analyzer_diagnostic.category-Style.severity = none
77+
```
78+
79+
For more information, see [How to suppress code analysis warnings](../suppress-warnings.md).
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
title: "IDE0370: Remove unnecessary suppression"
3+
description: "Learn about code analysis rule IDE0370: Remove unnecessary suppression"
4+
ms.date: 11/08/2025
5+
f1_keywords:
6+
- IDE0370
7+
helpviewer_keywords:
8+
- IDE0370
9+
dev_langs:
10+
- CSharp
11+
ai-usage: ai-assisted
12+
---
13+
# Remove unnecessary suppression (IDE0370)
14+
15+
| Property | Value |
16+
|--------------------------|--------------------------------------------------|
17+
| **Rule ID** | IDE0370 |
18+
| **Title** | Remove unnecessary suppression |
19+
| **Category** | Style |
20+
| **Subcategory** | Unnecessary code rules (suppression preferences) |
21+
| **Applicable languages** | C# |
22+
| **Options** | None |
23+
24+
## Overview
25+
26+
This rule identifies unnecessary nullable warning suppressions using the [null-forgiving operator](../../../csharp/language-reference/operators/null-forgiving.md) (`!`). The null-forgiving operator tells the compiler that the value is not null, which suppresses warnings for nullable reference types. However, when the compiler can already determine that a value is not null, the null-forgiving operator is unnecessary and can be removed.
27+
28+
## Example
29+
30+
```csharp
31+
// Code with violations.
32+
#nullable enable
33+
34+
void ProcessValue()
35+
{
36+
List<string> names = new()!;
37+
}
38+
39+
// Fixed code.
40+
#nullable enable
41+
42+
void ProcessValue()
43+
{
44+
List<string> names = new(); // No suppression needed.
45+
}
46+
```
47+
48+
## Suppress a warning
49+
50+
If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
51+
52+
```csharp
53+
#pragma warning disable IDE0370
54+
// The code that's violating the rule is on this line.
55+
#pragma warning restore IDE0370
56+
```
57+
58+
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md).
59+
60+
```ini
61+
[*.{cs,vb}]
62+
dotnet_diagnostic.IDE0370.severity = none
63+
```
64+
65+
To disable all of the code-style rules, set the severity for the category `Style` to `none` in the [configuration file](../configuration-files.md).
66+
67+
```ini
68+
[*.{cs,vb}]
69+
dotnet_analyzer_diagnostic.category-Style.severity = none
70+
```
71+
72+
For more information, see [How to suppress code analysis warnings](../suppress-warnings.md).
73+
74+
## See also
75+
76+
- [Nullable reference types](../../../csharp/nullable-references.md)
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
title: "IDE0380: Remove unnecessary 'unsafe' modifier"
3+
description: "Learn about code analysis rule IDE0380: Remove unnecessary 'unsafe' modifier"
4+
ms.date: 11/08/2025
5+
f1_keywords:
6+
- IDE0380
7+
helpviewer_keywords:
8+
- IDE0380
9+
dev_langs:
10+
- CSharp
11+
ai-usage: ai-assisted
12+
---
13+
# Remove unnecessary `unsafe` modifier (IDE0380)
14+
15+
| Property | Value |
16+
|--------------------------|-----------------------------------------------|
17+
| **Rule ID** | IDE0380 |
18+
| **Title** | Remove unnecessary `unsafe` modifier |
19+
| **Category** | Style |
20+
| **Subcategory** | Unnecessary code rules (modifier preferences) |
21+
| **Applicable languages** | C# |
22+
| **Options** | None |
23+
24+
## Overview
25+
26+
This rule identifies code blocks, methods, types, or other declarations marked with the `unsafe` modifier that don't actually contain any unsafe operations. The `unsafe` modifier allows the use of pointers and other unsafe code features, but when those features aren't being used, the modifier is unnecessary and should be removed for code clarity.
27+
28+
## Example
29+
30+
```csharp
31+
// Code with violations.
32+
33+
// Unnecessary, no unsafe operations.
34+
unsafe class MyClass
35+
{
36+
public void Method()
37+
{
38+
var x = 5;
39+
}
40+
}
41+
42+
// Unnecessary, no unsafe operations.
43+
unsafe void ProcessData(int value)
44+
{
45+
Console.WriteLine(value);
46+
}
47+
48+
// Fixed code.
49+
class MyClass
50+
{
51+
public void Method()
52+
{
53+
var x = 5;
54+
}
55+
}
56+
57+
void ProcessData(int value)
58+
{
59+
Console.WriteLine(value);
60+
}
61+
62+
// Example where 'unsafe' is needed.
63+
unsafe class ValidUsage
64+
{
65+
int* pointer; // Pointer type requires 'unsafe'.
66+
}
67+
```
68+
69+
## Suppress a warning
70+
71+
If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
72+
73+
```csharp
74+
#pragma warning disable IDE0380
75+
// The code that's violating the rule is on this line.
76+
#pragma warning restore IDE0380
77+
```
78+
79+
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md).
80+
81+
```ini
82+
[*.{cs,vb}]
83+
dotnet_diagnostic.IDE0380.severity = none
84+
```
85+
86+
To disable all of the code-style rules, set the severity for the category `Style` to `none` in the [configuration file](../configuration-files.md).
87+
88+
```ini
89+
[*.{cs,vb}]
90+
dotnet_analyzer_diagnostic.category-Style.severity = none
91+
```
92+
93+
For more information, see [How to suppress code analysis warnings](../suppress-warnings.md).
94+
95+
## See also
96+
97+
- [Unsafe code, pointer types, and function pointers](../../../csharp/language-reference/unsafe-code.md)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ The following table list all the code-style rules by ID and [options](../code-st
146146
> | [IDE0330](ide0330.md) | Prefer 'System.Threading.Lock' | [csharp_prefer_system_threading_lock](ide0330.md#csharp_prefer_system_threading_lock) |
147147
> | [IDE0340](ide0340.md) | Use unbound generic type | [csharp_style_prefer_unbound_generic_type_in_nameof](ide0340.md#csharp_style_prefer_unbound_generic_type_in_nameof) |
148148
> | [IDE0350](ide0350.md) | Use implicitly typed lambda | [csharp_style_prefer_implicitly_typed_lambda_expression](ide0350.md#csharp_style_prefer_implicitly_typed_lambda_expression) |
149+
> | [IDE0360](ide0360.md) | Simplify property accessor | [csharp_style_prefer_simple_property_accessors](ide0360.md#csharp_style_prefer_simple_property_accessors) |
150+
> | [IDE0370](ide0370.md) | Remove unnecessary suppression | |
151+
> | [IDE0380](ide0380.md) | Remove unnecessary 'unsafe' modifier | |
149152
> | [IDE1005](ide1005.md) | Use conditional delegate call | [csharp_style_conditional_delegate_call](ide1005.md#csharp_style_conditional_delegate_call) |
150153
> | [IDE1006](naming-rules.md) | Naming styles | |
151154
> | [IDE2000](ide2000.md) | Avoid multiple blank lines | [dotnet_style_allow_multiple_blank_lines_experimental](ide2000.md#dotnet_style_allow_multiple_blank_lines_experimental)|

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Code-style language and unnecessary code rules
33
description: Learn about the different code-style rules for using C# and Visual Basic language constructs and for finding unnecessary code.
4-
ms.date: 03/25/2025
4+
ms.date: 11/07/2025
55
helpviewer_keywords:
66
- language code style rules [EditorConfig]
77
- language rules
@@ -172,6 +172,7 @@ C# style rules:
172172
- [Use collection expression for new (IDE0306)](ide0306.md)
173173
- [Use unbound generic type (IDE0340)](ide0340.md)
174174
- [Use implicitly typed lambda (IDE0350)](ide0350.md)
175+
- [Simplify property accessor (IDE0360)](ide0360.md)
175176

176177
Visual Basic style rules:
177178

@@ -205,6 +206,7 @@ C# style rules:
205206
- [Struct can be made 'readonly' (IDE0250)](ide0250.md)
206207
- [Member can be made 'readonly' (IDE0251)](ide0251.md)
207208
- [Make anonymous function static (IDE0320)](ide0320.md)
209+
- [Remove unnecessary `unsafe` modifier (IDE0380)](ide0380.md)
208210

209211
### New-line preferences
210212

@@ -259,6 +261,10 @@ C# style rules:
259261

260262
- [Remove unnecessary suppression (IDE0079)](ide0079.md)
261263

264+
C# style rules:
265+
266+
- [Remove unnecessary suppression (null-forgiving operator) (IDE0370)](ide0370.md)
267+
262268
### `This.` and `me.` preferences
263269

264270
.NET style rules (C# and Visual Basic):

docs/navigate/tools-diagnostics/toc.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3874,6 +3874,12 @@ items:
38743874
href: ../../fundamentals/code-analysis/style-rules/ide0340.md
38753875
- name: IDE0350
38763876
href: ../../fundamentals/code-analysis/style-rules/ide0350.md
3877+
- name: IDE0360
3878+
href: ../../fundamentals/code-analysis/style-rules/ide0360.md
3879+
- name: IDE0370
3880+
href: ../../fundamentals/code-analysis/style-rules/ide0370.md
3881+
- name: IDE0380
3882+
href: ../../fundamentals/code-analysis/style-rules/ide0380.md
38773883
- name: IDE1005
38783884
href: ../../fundamentals/code-analysis/style-rules/ide1005.md
38793885
- name: IDE2000

0 commit comments

Comments
 (0)