This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
This article describes two related rules,IDE0020 andIDE0038.
| Property | Value |
|---|---|
| Rule ID | IDE0020 |
| Title | Use pattern matching to avoidis check followed by a cast (with variable) |
| Category | Style |
| Subcategory | Language rules (pattern matching preferences) |
| Applicable languages | C# |
| Options | csharp_style_pattern_matching_over_is_with_cast_check |
| Property | Value |
|---|---|
| Rule ID | IDE0038 |
| Title | Use pattern matching to avoidis check followed by a cast (without variable) |
| Category | Style |
| Subcategory | Language rules (pattern matching preferences) |
| Applicable languages | C# |
| Options | csharp_style_pattern_matching_over_is_with_cast_check |
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.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.
| Property | Value | Description |
|---|---|---|
| Option name | csharp_style_pattern_matching_over_is_with_cast_check | |
| Option values | true | Prefer pattern matching instead ofis expressions with type casts. |
false | Disables the rule. | |
| Default option value | true |
// 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; ... }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 IDE0038To 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 = noneTo 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 = noneFor more information, seeHow to suppress code analysis warnings.
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?