|
| 1 | +--- |
| 2 | +title: "IDE0170: Simplify property pattern" |
| 3 | +description: "Learn about code analysis rule IDE0170: Simplify property pattern" |
| 4 | +ms.date: 06/29/2022 |
| 5 | +ms.topic: reference |
| 6 | +f1_keywords: |
| 7 | +- IDE0170 |
| 8 | +- csharp_style_prefer_extended_property_pattern |
| 9 | +helpviewer_keywords: |
| 10 | +- IDE0170 |
| 11 | +- csharp_style_prefer_extended_property_pattern |
| 12 | +author: gewarren |
| 13 | +ms.author: gewarren |
| 14 | +--- |
| 15 | +# Simplify property pattern (IDE0170) |
| 16 | + |
| 17 | +| Property | Value | |
| 18 | +|--------------------------|--------------------------------------------------| |
| 19 | +| **Rule ID** | IDE0170 | |
| 20 | +| **Title** | Simplify property pattern | |
| 21 | +| **Category** | Style | |
| 22 | +| **Subcategory** | Language rules (pattern matching preferences) | |
| 23 | +| **Applicable languages** | C# | |
| 24 | +| **Options** | `csharp_style_prefer_extended_property_pattern` | |
| 25 | + |
| 26 | +## Overview |
| 27 | + |
| 28 | +This style rule flags the use of a nested pattern in a [property pattern](../../../csharp/language-reference/operators/patterns.md#property-pattern). A nested pattern can be simplified to use an extended property pattern in which property subpatterns are used to reference nested members. Extended property patterns improve code readability. |
| 29 | + |
| 30 | +## Options |
| 31 | + |
| 32 | +Options specify the behavior that you want the rule to enforce. For information about configuring options, see [Option format](language-rules.md#option-format). |
| 33 | + |
| 34 | +### csharp_style_prefer_extended_property_pattern |
| 35 | + |
| 36 | +| Property | Value | Description | |
| 37 | +|--------------------------|-----------------------------------------------|---------------------------------------| |
| 38 | +| **Option name** | csharp_style_prefer_extended_property_pattern | | |
| 39 | +| **Option values** | `true` | Prefer the extended property pattern. | |
| 40 | +| | `false` | Disables the rule. | |
| 41 | +| **Default option value** | `true` | | |
| 42 | + |
| 43 | +## Example |
| 44 | + |
| 45 | +```csharp |
| 46 | +public record Point(int X, int Y); |
| 47 | +public record Segment(Point Start, Point End); |
| 48 | + |
| 49 | +// Violates IDE0170. |
| 50 | +static bool IsEndOnXAxis(Segment segment) => |
| 51 | + segment is { Start: { Y: 0 } } or { End: { Y: 0 } }; |
| 52 | + |
| 53 | +// Fixed code. |
| 54 | +static bool IsEndOnXAxis(Segment segment) => |
| 55 | + segment is { Start.Y: 0 } or { End.Y: 0 }; |
| 56 | +``` |
| 57 | + |
| 58 | +## Suppress a warning |
| 59 | + |
| 60 | +If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule. |
| 61 | + |
| 62 | +```csharp |
| 63 | +#pragma warning disable IDE0170 |
| 64 | +// The code that's violating the rule is on this line. |
| 65 | +#pragma warning restore IDE0170 |
| 66 | +``` |
| 67 | + |
| 68 | +To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md). |
| 69 | + |
| 70 | +```ini |
| 71 | +[*.{cs,vb}] |
| 72 | +dotnet_diagnostic.IDE0170.severity = none |
| 73 | +``` |
| 74 | + |
| 75 | +To disable all of the code-style rules, set the severity for the category `Style` to `none` in the [configuration file](../configuration-files.md). |
| 76 | + |
| 77 | +```ini |
| 78 | +[*.{cs,vb}] |
| 79 | +dotnet_analyzer_diagnostic.category-Style.severity = none |
| 80 | +``` |
| 81 | + |
| 82 | +For more information, see [How to suppress code analysis warnings](../suppress-warnings.md). |
| 83 | + |
| 84 | +## See also |
| 85 | + |
| 86 | +- [Property pattern](../../../csharp/language-reference/operators/patterns.md#property-pattern) |
| 87 | +- [Pattern matching preferences](pattern-matching-preferences.md) |
| 88 | +- [Code style language rules](language-rules.md) |
| 89 | +- [Code style rules reference](index.md) |
0 commit comments