Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

Use pattern matching to avoid 'is' check followed by a cast (IDE0020 and IDE0038)

Feedback

In this article

This article describes two related rules,IDE0020 andIDE0038.

PropertyValue
Rule IDIDE0020
TitleUse pattern matching to avoidis check followed by a cast (with variable)
CategoryStyle
SubcategoryLanguage rules (pattern matching preferences)
Applicable languagesC#
Optionscsharp_style_pattern_matching_over_is_with_cast_check
PropertyValue
Rule IDIDE0038
TitleUse pattern matching to avoidis check followed by a cast (without variable)
CategoryStyle
SubcategoryLanguage rules (pattern matching preferences)
Applicable languagesC#
Optionscsharp_style_pattern_matching_over_is_with_cast_check

Overview

This style rule concerns the use of C#pattern matching, for example,o is int i, over anis check followed by a cast, for example,if (o is int) { ... (int)o ... }. Enable eitherIDE0020 orIDE0038 based on whether or not the cast expression should be saved into a separate local variable:

  • IDE0020: Cast expressionis saved into a local variable. For example,if (o is int) { var i = (int)o; } saves the result of(int)o in a local variable.
  • IDE0038: Cast expressionis not saved into a local variable. For example,if (o is int) { if ((int)o == 1) { ... } } does not save the result of(int)o into a local variable.

Options

Set the value of the associated option for this rule to specify whether pattern matching oris check followed by a type cast is preferred.

For more information about configuring options, seeOption format.

csharp_style_pattern_matching_over_is_with_cast_check

PropertyValueDescription
Option namecsharp_style_pattern_matching_over_is_with_cast_check
Option valuestruePrefer pattern matching instead ofis expressions with type casts.
falseDisables the rule.
Default option valuetrue
// csharp_style_pattern_matching_over_is_with_cast_check = trueif (o is int i) {...}// csharp_style_pattern_matching_over_is_with_cast_check = falseif (o is int) {var i = (int)o; ... }

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.

#pragma warning disable IDE0020 // Or IDE0038// The code that's violating the rule is on this line.#pragma warning restore IDE0020 // Or IDE0038

To disable the rule for a file, folder, or project, set its severity tonone in theconfiguration file.

[*.{cs,vb}]dotnet_diagnostic.IDE0020.severity = nonedotnet_diagnostic.IDE0038.severity = none

To disable all of the code-style rules, set the severity for the categoryStyle tonone in theconfiguration file.

[*.{cs,vb}]dotnet_analyzer_diagnostic.category-Style.severity = none

For more information, seeHow to suppress code analysis warnings.

See also

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?