diff --git a/docs/fundamentals/code-analysis/code-style-rule-options.md b/docs/fundamentals/code-analysis/code-style-rule-options.md index c4a76c7533bb9..11345ce33aa20 100644 --- a/docs/fundamentals/code-analysis/code-style-rule-options.md +++ b/docs/fundamentals/code-analysis/code-style-rule-options.md @@ -96,8 +96,10 @@ dotnet_style_prefer_foreach_explicit_cast_in_source = when_strongly_typed dotnet_style_prefer_inferred_anonymous_type_member_names = true dotnet_style_prefer_inferred_tuple_names = true dotnet_style_prefer_is_null_check_over_reference_equality_method = true +dotnet_style_prefer_non_hidden_explicit_cast_in_source = true dotnet_style_prefer_simplified_boolean_expressions = true dotnet_style_prefer_simplified_interpolation = true +dotnet_prefer_system_hash_code = true # Field preferences dotnet_style_readonly_field = true diff --git a/docs/fundamentals/code-analysis/style-rules/ide0070.md b/docs/fundamentals/code-analysis/style-rules/ide0070.md index 3ede93b30cd1c..96276c81cbd14 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0070.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0070.md @@ -1,11 +1,13 @@ --- title: "IDE0070: Use 'System.HashCode.Combine'" description: "Learn about code analysis rule IDE0070: Use 'System.HashCode.Combine'" -ms.date: 09/30/2020 +ms.date: 04/10/2026 f1_keywords: - IDE0070 +- dotnet_prefer_system_hash_code helpviewer_keywords: - IDE0070 +- dotnet_prefer_system_hash_code author: gewarren ms.author: gewarren dev_langs: @@ -21,6 +23,7 @@ dev_langs: | **Category** | Style | | **Subcategory** | Language rules (expression-level preferences) | | **Applicable languages** | C# and Visual Basic | +| **Options** | `dotnet_prefer_system_hash_code` | ## Overview @@ -28,7 +31,16 @@ This rule recommends the use of the for hash code computation. | +| | `false` | Don't prefer to use `System.HashCode.Combine` for hash code computation. | +| **Default option value** | `true` | | ## Example diff --git a/docs/fundamentals/code-analysis/style-rules/ide0221.md b/docs/fundamentals/code-analysis/style-rules/ide0221.md new file mode 100644 index 0000000000000..d3e59c05e38ba --- /dev/null +++ b/docs/fundamentals/code-analysis/style-rules/ide0221.md @@ -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) diff --git a/docs/fundamentals/code-analysis/style-rules/index.md b/docs/fundamentals/code-analysis/style-rules/index.md index e9837df7a16f6..9f041c435ff83 100644 --- a/docs/fundamentals/code-analysis/style-rules/index.md +++ b/docs/fundamentals/code-analysis/style-rules/index.md @@ -95,7 +95,7 @@ The following table list all the code-style rules by ID and [options](../code-st > | [IDE0064](ide0064.md) | Make struct fields writable | | > | [IDE0065](ide0065.md) | `using` directive placement | [csharp_using_directive_placement](ide0065.md#csharp_using_directive_placement) | > | [IDE0066](ide0066.md) | Use switch expression | [csharp_style_prefer_switch_expression](ide0066.md#csharp_style_prefer_switch_expression) | -> | [IDE0070](ide0070.md) | Use | | +> | [IDE0070](ide0070.md) | Use | [dotnet_prefer_system_hash_code](ide0070.md#dotnet_prefer_system_hash_code) | > | [IDE0071](ide0071.md) | Simplify interpolation | [dotnet_style_prefer_simplified_interpolation](ide0071.md#dotnet_style_prefer_simplified_interpolation) | > | [IDE0072](ide0072.md) | Add missing cases to switch expression | | > | [IDE0073](ide0073.md) | Use file header | [file_header_template](ide0073.md#file_header_template) | @@ -126,6 +126,7 @@ The following table list all the code-style rules by ID and [options](../code-st > | [IDE0210](ide0210.md) | Convert to top-level statements | [csharp_style_prefer_top_level_statements](ide0210.md#csharp_style_prefer_top_level_statements) | > | [IDE0211](ide0211.md) | Convert to 'Program.Main' style program | [csharp_style_prefer_top_level_statements](ide0211.md#csharp_style_prefer_top_level_statements) | > | [IDE0220](ide0220.md) | Add explicit cast in foreach loop | [dotnet_style_prefer_foreach_explicit_cast_in_source](ide0220.md#dotnet_style_prefer_foreach_explicit_cast_in_source) | +> | [IDE0221](ide0221.md) | Add explicit cast | [dotnet_style_prefer_non_hidden_explicit_cast_in_source](ide0221.md#dotnet_style_prefer_non_hidden_explicit_cast_in_source) | > | [IDE0230](ide0230.md) | Use UTF-8 string literal | [csharp_style_prefer_utf8_string_literals](ide0230.md#csharp_style_prefer_utf8_string_literals) | > | [IDE0240](ide0240.md) | Nullable directive is redundant | | > | [IDE0241](ide0241.md) | Nullable directive is unnecessary | | diff --git a/docs/fundamentals/code-analysis/style-rules/language-rules.md b/docs/fundamentals/code-analysis/style-rules/language-rules.md index d926f218bf1c5..c6f2af1738f4a 100644 --- a/docs/fundamentals/code-analysis/style-rules/language-rules.md +++ b/docs/fundamentals/code-analysis/style-rules/language-rules.md @@ -160,6 +160,7 @@ C# style rules: - [Prefer 'null' check over type check (IDE0150)](ide0150.md) - [Use tuple to swap values (IDE0180)](ide0180.md) - [Add explicit cast in foreach loop (IDE0220)](ide0220.md) +- [Add explicit cast (IDE0221)](ide0221.md) - [Use UTF-8 string literal (IDE0230)](ide0230.md) - [Nullable directive is redundant (IDE0240)](ide0240.md) - [Nullable directive is unnecessary (IDE0241)](ide0241.md) diff --git a/docs/navigate/tools-diagnostics/toc.yml b/docs/navigate/tools-diagnostics/toc.yml index 078779fa55ac0..a95a2784c5e50 100644 --- a/docs/navigate/tools-diagnostics/toc.yml +++ b/docs/navigate/tools-diagnostics/toc.yml @@ -3854,6 +3854,8 @@ items: href: ../../fundamentals/code-analysis/style-rules/ide0211.md - name: IDE0220 href: ../../fundamentals/code-analysis/style-rules/ide0220.md + - name: IDE0221 + href: ../../fundamentals/code-analysis/style-rules/ide0221.md - name: IDE0230 href: ../../fundamentals/code-analysis/style-rules/ide0230.md - name: IDE0240