Skip to content

Commit 03ce644

Browse files
authored
add ide0290 (#38008)
1 parent 26cab0f commit 03ce644

File tree

5 files changed

+86
-3
lines changed

5 files changed

+86
-3
lines changed

docs/csharp/programming-guide/classes-and-structs/instance-constructors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Beginning in C# 12, you can declare a *primary constructor* in classes and struc
4141

4242
The parameters to a primary constructor are in scope in the entire body of the declaring type. They can initialize properties or fields. They can be used as variables in methods or local functions. They can be passed to a base constructor.
4343

44-
A primary constructor indicates that these parameters are necessary for any instance of a type. Any explicitly written constructor must use the `this(...)` initializer syntax to invoke the primary constructor. That ensures that the primary constructor parameters are definitely assigned by all constructors. For any `class` type, including `record class` types, the implicit parameterless constructor isn't emitted when a primary constructor is present. For any `struct` type, including `record struct` types, the implicit parameterless constructor is always emitted, and always initializes all fields, including primary constructor parameters, to the 0-bit pattern. If you write an explicit parameterless constructor, it must invoke the primary constructor. In that case, you can specify a different value for the primary constructor parameters. The following code shows examples of primary constructors.
44+
A primary constructor indicates that these parameters are necessary for any instance of the type. Any explicitly written constructor must use the `this(...)` initializer syntax to invoke the primary constructor. That ensures that the primary constructor parameters are definitely assigned by all constructors. For any `class` type, including `record class` types, the implicit parameterless constructor isn't emitted when a primary constructor is present. For any `struct` type, including `record struct` types, the implicit parameterless constructor is always emitted, and always initializes all fields, including primary constructor parameters, to the 0-bit pattern. If you write an explicit parameterless constructor, it must invoke the primary constructor. In that case, you can specify a different value for the primary constructor parameters. The following code shows examples of primary constructors.
4545

4646
:::code language="csharp" source="./snippets/instance-constructors/widgets/Program.cs" id="DerivedPrimaryConstructor":::
4747

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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).

docs/fundamentals/code-analysis/style-rules/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Code-style rules overview
33
description: Learn about the different .NET code-style rules and categories.
4-
ms.date: 07/22/2023
4+
ms.date: 11/10/2023
55
author: gewarren
66
ms.author: gewarren
77
---
@@ -132,6 +132,7 @@ The following table list all the code-style rules by ID and [options](../code-st
132132
> | [IDE0260](ide0078-ide0260.md) | Use pattern matching | [csharp_style_pattern_matching_over_as_with_null_check](ide0078-ide0260.md#csharp_style_pattern_matching_over_as_with_null_check-ide0260) |
133133
> | [IDE0270](ide0029-ide0030-ide0270.md) | Null check can be simplified | [dotnet_style_coalesce_expression](ide0029-ide0030-ide0270.md#dotnet_style_coalesce_expression) |
134134
> | [IDE0280](ide0280.md) | Use `nameof` | |
135+
> | [IDE0290](ide0290.md) | Use primary constructor | [csharp_style_prefer_primary_constructors](ide0290.md#csharp_style_prefer_primary_constructors) |
135136
> | [IDE1005](ide1005.md) | Use conditional delegate call | [csharp_style_conditional_delegate_call](ide1005.md#csharp_style_conditional_delegate_call) |
136137
> | [IDE1006](naming-rules.md) | Naming styles | |
137138

docs/fundamentals/code-analysis/style-rules/language-rules.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Code-style language and unnecessary code rules
33
description: Learn about the different code-style rules for using C# and Visual Basic language constructs and for finding unnecessary code.
4-
ms.date: 07/22/2023
4+
ms.date: 11/10/2023
55
helpviewer_keywords:
66
- language code style rules [EditorConfig]
77
- language rules
@@ -91,6 +91,7 @@ C# style rules:
9191
- [Remove unnecessary lambda expression (IDE0200)](ide0200.md)
9292
- [Convert to top-level statements (IDE0210)](ide0210.md)
9393
- [Convert to 'Program.Main' style program (IDE0211)](ide0211.md)
94+
- [Use primary constructor (IDE0290)](ide0290.md)
9495

9596
### Expression-bodied members
9697

docs/navigate/tools-diagnostics/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,6 +1544,8 @@ items:
15441544
href: ../../fundamentals/code-analysis/style-rules/ide0029-ide0030-ide0270.md
15451545
- name: IDE0280
15461546
href: ../../fundamentals/code-analysis/style-rules/ide0280.md
1547+
- name: IDE0290
1548+
href: ../../fundamentals/code-analysis/style-rules/ide0290.md
15471549
- name: IDE1005
15481550
href: ../../fundamentals/code-analysis/style-rules/ide1005.md
15491551
- name: Miscellaneous rules

0 commit comments

Comments
 (0)