-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Document dotnet_prefer_system_hash_code (IDE0070) and new IDE0221 rule #52975
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| --- | ||
| title: "IDE0221: Add explicit cast" | ||
| description: "Learn about code analysis rule IDE0221: Add explicit cast" | ||
| ms.date: 04/10/2026 | ||
| f1_keywords: | ||
| - IDE0221 | ||
| - dotnet_style_prefer_non_hidden_explicit_cast_in_source | ||
| helpviewer_keywords: | ||
| - IDE0221 | ||
| - dotnet_style_prefer_non_hidden_explicit_cast_in_source | ||
| dev_langs: | ||
| - CSharp | ||
| --- | ||
| # Add explicit cast (IDE0221) | ||
|
|
||
| | Property | Value | | ||
| |--------------------------|--------------------------------------------------------------------| | ||
| | **Rule ID** | IDE0221 | | ||
| | **Title** | Add explicit cast | | ||
| | **Category** | Style | | ||
| | **Subcategory** | Language rules (expression-level preferences) | | ||
| | **Applicable languages** | C# | | ||
| | **Options** | `dotnet_style_prefer_non_hidden_explicit_cast_in_source` | | ||
|
|
||
| ## Overview | ||
|
|
||
| This rule flags explicit casts in source code where the compiler inserts an additional hidden explicit cast. Both the visible and hidden casts can fail at runtime for different reasons. When this rule flags such code, it recommends adding the intermediate cast explicitly in source to make the code's intent clear. | ||
|
|
||
| For example, if you write `(Derived)x` where `x` is of a type that requires two explicit conversions—first to a base type and then to the derived type—only one cast is visible in source. The compiler inserts the intermediate cast without any indication in the source. This rule suggests writing both casts explicitly: `(Derived)(Base)x`. | ||
|
|
||
| ## Options | ||
|
|
||
| Options specify the behavior that you want the rule to enforce. For information about configuring options, see [Option format](language-rules.md#option-format). | ||
|
|
||
| ### dotnet_style_prefer_non_hidden_explicit_cast_in_source | ||
|
|
||
| | Property | Value | Description | | ||
| |--------------------------|----------------------------------------------------------|---------------------------------------------------------------------------------------| | ||
| | **Option name** | dotnet_style_prefer_non_hidden_explicit_cast_in_source | | | ||
| | **Option values** | `true` | Prefer to make all intermediate explicit casts visible in source code. | | ||
| | | `false` | Don't prefer to make all intermediate explicit casts visible in source code. | | ||
| | **Default option value** | `true` | | | ||
|
|
||
| ## Example | ||
|
|
||
| ```csharp | ||
| class Base { } | ||
| class Derived : Base { } | ||
|
|
||
| class Castable | ||
| { | ||
| public static explicit operator Base(Castable c) => new Base(); | ||
| } | ||
|
|
||
| class C | ||
| { | ||
| void M() | ||
| { | ||
| // Code with violation: the compiler inserts a hidden (Base) cast. | ||
| var v = (Derived)new Castable(); | ||
|
|
||
| // Fixed code: both casts are explicit in source. | ||
| var v2 = (Derived)(Base)new Castable(); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## 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. | ||
|
|
||
| ```csharp | ||
| #pragma warning disable IDE0221 | ||
| // The code that's violating the rule is on this line. | ||
| #pragma warning restore IDE0221 | ||
| ``` | ||
|
|
||
| To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md). | ||
|
|
||
| ```ini | ||
| [*.cs] | ||
| dotnet_diagnostic.IDE0221.severity = none | ||
| ``` | ||
|
|
||
| To disable all of the code-style rules, set the severity for the category `Style` to `none` in the [configuration file](../configuration-files.md). | ||
|
|
||
| ```ini | ||
| [*.cs] | ||
| dotnet_analyzer_diagnostic.category-Style.severity = none | ||
| ``` | ||
|
|
||
| For more information, see [How to suppress code analysis warnings](../suppress-warnings.md). | ||
|
|
||
| ## See also | ||
|
|
||
| - [Add explicit cast in foreach loop (IDE0220)](ide0220.md) | ||
| - [Code style language rules](language-rules.md) | ||
| - [Code style rules reference](index.md) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.