|
| 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) |
0 commit comments