|
| 1 | +--- |
| 2 | +title: "IDE0290: Use primary constructor" |
| 3 | +description: "Learn about code analysis rule IDE0290: Use primary constructor" |
| 4 | +ms.date: 11/10/2023 |
| 5 | +f1_keywords: |
| 6 | +- IDE0290 |
| 7 | +helpviewer_keywords: |
| 8 | +- IDE0290 |
| 9 | +dev_langs: |
| 10 | +- CSharp |
| 11 | +--- |
| 12 | +# Use primary constructor (IDE0290) |
| 13 | + |
| 14 | +| Property | Value | |
| 15 | +|--------------------------|-----------------------------------------------| |
| 16 | +| **Rule ID** | IDE0290 | |
| 17 | +| **Title** | Use primary constructor | |
| 18 | +| **Category** | Style | |
| 19 | +| **Subcategory** | Language rules (code block preferences) | |
| 20 | +| **Applicable languages** | C# 12+ | |
| 21 | +| **Options** | `csharp_style_prefer_primary_constructors` | |
| 22 | + |
| 23 | +## Overview |
| 24 | + |
| 25 | +This rule flags classes that can use a [primary constructor](../../../csharp/programming-guide/classes-and-structs/instance-constructors.md#primary-constructors) instead of a separate constructor definition. You define a primary constructor by placing any constructor parameters in parentheses following the type name. A primary constructor indicates that these parameters are necessary for any instance of the type. |
| 26 | + |
| 27 | +## Options |
| 28 | + |
| 29 | +Options specify the behavior that you want the rule to enforce. For information about configuring options, see [Option format](language-rules.md#option-format). |
| 30 | + |
| 31 | +### csharp_style_prefer_primary_constructors |
| 32 | + |
| 33 | +| Property | Value | Description | |
| 34 | +|--------------------------|------------------------------------------|-------------------------------------| |
| 35 | +| **Option name** | csharp_style_prefer_primary_constructors | | |
| 36 | +| **Option values** | `true` | Prefer to use primary constructors. | |
| 37 | +| | `false` | Disables the rule. | |
| 38 | +| **Default option value** | `true` | | |
| 39 | + |
| 40 | +## Example |
| 41 | + |
| 42 | +```csharp |
| 43 | +// Code with violations. |
| 44 | +class C |
| 45 | +{ |
| 46 | + public C(int i) { } |
| 47 | +} |
| 48 | + |
| 49 | +// Fixed code. |
| 50 | +class C(int i) |
| 51 | +{ |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +## Suppress a warning |
| 56 | + |
| 57 | +If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule. |
| 58 | + |
| 59 | +```csharp |
| 60 | +#pragma warning disable IDE0290 |
| 61 | +// The code that's violating the rule is on this line. |
| 62 | +#pragma warning restore IDE0290 |
| 63 | +``` |
| 64 | + |
| 65 | +To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md). |
| 66 | + |
| 67 | +```ini |
| 68 | +[*.{cs,vb}] |
| 69 | +dotnet_diagnostic.IDE0290.severity = none |
| 70 | +``` |
| 71 | + |
| 72 | +To disable all of the code-style rules, set the severity for the category `Style` to `none` in the [configuration file](../configuration-files.md). |
| 73 | + |
| 74 | +```ini |
| 75 | +[*.{cs,vb}] |
| 76 | +dotnet_analyzer_diagnostic.category-Style.severity = none |
| 77 | +``` |
| 78 | + |
| 79 | +For more information, see [How to suppress code analysis warnings](../suppress-warnings.md). |
0 commit comments