Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/fundamentals/code-analysis/code-style-rule-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions docs/fundamentals/code-analysis/style-rules/ide0070.md
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -21,14 +23,24 @@ dev_langs:
| **Category** | Style |
| **Subcategory** | Language rules (expression-level preferences) |
| **Applicable languages** | C# and Visual Basic |
| **Options** | `dotnet_prefer_system_hash_code` |

## Overview

This rule recommends the use of the <xref:System.HashCode.Combine*?displayProperty=fullName> method to compute a hash code instead of using custom hash code computation logic.

## Options

This rule has no associated code-style 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_prefer_system_hash_code

| Property | Value | Description |
|--------------------------|----------------------------------|-------------------------------------------------------------------------------------------|
| **Option name** | dotnet_prefer_system_hash_code | |
| **Option values** | `true` | Prefer to use <xref:System.HashCode.Combine*?displayProperty=fullName> for hash code computation. |
| | `false` | Don't prefer to use `System.HashCode.Combine` for hash code computation. |
| **Default option value** | `true` | |

## Example

Expand Down
98 changes: 98 additions & 0 deletions docs/fundamentals/code-analysis/style-rules/ide0221.md
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)
3 changes: 2 additions & 1 deletion docs/fundamentals/code-analysis/style-rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <xref:System.HashCode.Combine*?displayProperty=fullName> | |
> | [IDE0070](ide0070.md) | Use <xref:System.HashCode.Combine*?displayProperty=fullName> | [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) |
Expand Down Expand Up @@ -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 | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions docs/navigate/tools-diagnostics/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading