Skip to content

Commit 5dc0c81

Browse files
authored
Add docs for IDE0121, IDE0340, IDE0350 (#45510)
1 parent 687498d commit 5dc0c81

File tree

6 files changed

+237
-0
lines changed

6 files changed

+237
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
title: "IDE0121: Simplify LINQ type check and cast"
3+
description: "Learn about code analysis rule IDE0121: Simplify LINQ type check and cast"
4+
ms.date: 03/25/2025
5+
f1_keywords:
6+
- IDE0121
7+
helpviewer_keywords:
8+
- IDE0121
9+
dev_langs:
10+
- CSharp
11+
---
12+
# Simplify LINQ type check and cast (IDE0121)
13+
14+
| Property | Value |
15+
|--------------------------|-------------------------------------------------------|
16+
| **Rule ID** | IDE0121 |
17+
| **Title** | Simplify LINQ type check and cast |
18+
| **Category** | Style |
19+
| **Subcategory** | Unnecessary code rules (expression-level preferences) |
20+
| **Applicable languages** | C# and Visual Basic |
21+
22+
## Overview
23+
24+
This rule flags LINQ expressions where the type of elements of a sequence are checked (by calling <xref:System.Linq.Enumerable.Where``1(System.Collections.Generic.IEnumerable{``0},System.Func{``0,System.Boolean})>) and then cast to that type (by calling either <xref:System.Linq.Enumerable.Cast``1(System.Collections.IEnumerable)> or <xref:System.Linq.Enumerable.Select``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1})>). The code fixer converts these expressions to call <xref:System.Linq.Enumerable.OfType``1(System.Collections.IEnumerable)> instead.
25+
26+
## Options
27+
28+
This rule has no associated code-style options.
29+
30+
## Example
31+
32+
```csharp
33+
// Code with violations.
34+
IEnumerable<int> y = objects.Where(a => a is int).Cast<int>();
35+
IEnumerable<int> z = objects.Where(a => a is int).Select(a => (int)a);
36+
37+
// Fixed code.
38+
IEnumerable<int> y = objects.OfType<int>();
39+
IEnumerable<int> z = objects.OfType<int>();
40+
```
41+
42+
## Suppress a warning
43+
44+
If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
45+
46+
```csharp
47+
#pragma warning disable IDE0121
48+
// The code that's violating the rule is on this line.
49+
#pragma warning restore IDE0121
50+
```
51+
52+
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md).
53+
54+
```ini
55+
[*.{cs,vb}]
56+
dotnet_diagnostic.IDE0121.severity = none
57+
```
58+
59+
To disable all of the code-style rules, set the severity for the category `Style` to `none` in the [configuration file](../configuration-files.md).
60+
61+
```ini
62+
[*.{cs,vb}]
63+
dotnet_analyzer_diagnostic.category-Style.severity = none
64+
```
65+
66+
For more information, see [How to suppress code analysis warnings](../suppress-warnings.md).
67+
68+
## See also
69+
70+
- [Language rules](language-rules.md)
71+
- [Code style rules reference](index.md)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
title: "IDE0340: Use unbound generic type"
3+
description: "Learn about code analysis rule IDE0340: Use unbound generic type"
4+
ms.date: 03/25/2025
5+
f1_keywords:
6+
- IDE0340
7+
helpviewer_keywords:
8+
- IDE0340
9+
dev_langs:
10+
- CSharp
11+
---
12+
# Use unbound generic type (IDE0340)
13+
14+
| Property | Value |
15+
|--------------------------|------------------------------------------------------|
16+
| **Rule ID** | IDE0340 |
17+
| **Title** | Use unbound generic type |
18+
| **Category** | Style |
19+
| **Subcategory** | Language rules (expression-level preferences) |
20+
| **Applicable languages** | C# 14+ |
21+
| **Options** | `csharp_style_prefer_unbound_generic_type_in_nameof` |
22+
23+
## Overview
24+
25+
This rule flags places where the operand of a [nameof expression](../../../csharp/language-reference/operators/nameof.md) is a bound generic type (for example, `List<int>`). The code fixer offers to convert the operand to an unbound type (for example, `List<>`).
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_unbound_generic_type_in_nameof
32+
33+
| Property | Value | Description |
34+
|--------------------------|----------------------------------------------------|-----------------------------------------|
35+
| **Option name** | csharp_style_prefer_unbound_generic_type_in_nameof | |
36+
| **Option values** | `true` | Prefer unbound generic type in `nameof` expression |
37+
| | `false` | Disables the rule |
38+
| **Default option value** | `true` | |
39+
40+
## Example
41+
42+
```csharp
43+
// Code with violations.
44+
string name = nameof(List<int>);
45+
```
46+
47+
```csharp
48+
// Fixed code.
49+
string name = nameof(List<>);
50+
```
51+
52+
## Suppress a warning
53+
54+
If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
55+
56+
```csharp
57+
#pragma warning disable IDE0340
58+
// The code that's violating the rule is on this line.
59+
#pragma warning restore IDE0340
60+
```
61+
62+
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md).
63+
64+
```ini
65+
[*.{cs,vb}]
66+
dotnet_diagnostic.IDE0340.severity = none
67+
```
68+
69+
To disable all of the code-style rules, set the severity for the category `Style` to `none` in the [configuration file](../configuration-files.md).
70+
71+
```ini
72+
[*.{cs,vb}]
73+
dotnet_analyzer_diagnostic.category-Style.severity = none
74+
```
75+
76+
For more information, see [How to suppress code analysis warnings](../suppress-warnings.md).
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
title: "IDE0350: Use implicitly typed lambda"
3+
description: "Learn about code analysis rule IDE0350: Use implicitly typed lambda"
4+
ms.date: 03/25/2025
5+
f1_keywords:
6+
- IDE0350
7+
helpviewer_keywords:
8+
- IDE0350
9+
dev_langs:
10+
- CSharp
11+
---
12+
# Use implicitly typed lambda (IDE0350)
13+
14+
| Property | Value |
15+
|--------------------------|----------------------------------------------------------|
16+
| **Rule ID** | IDE0350 |
17+
| **Title** | Use implicitly typed lambda |
18+
| **Category** | Style |
19+
| **Subcategory** | Language rules (expression-level preferences) |
20+
| **Applicable languages** | C# 14+ |
21+
| **Options** | `csharp_style_prefer_implicitly_typed_lambda_expression` |
22+
23+
## Overview
24+
25+
This rule flags places where an explicitly typed [lambda expression](../../../csharp/language-reference/operators/lambda-expressions.md) can be converted to an implicitly typed lambda expression.
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_implicitly_typed_lambda_expression
32+
33+
| Property | Value | Description |
34+
|--------------------------|--------------------------------------------------------|------------------------------------------|
35+
| **Option name** | csharp_style_prefer_implicitly_typed_lambda_expression | |
36+
| **Option values** | `true` | Prefer to use an implicitly typed lambda |
37+
| | `false` | Disables the rule |
38+
| **Default option value** | `true` | |
39+
40+
## Example
41+
42+
```csharp
43+
// Code with violations.
44+
Action<int> a = (int x) => { };
45+
Delegate b = (Action<int>)( (int x) => { } );
46+
```
47+
48+
```csharp
49+
// Fixed code.
50+
Action<int> a = x => { };
51+
Delegate b = (Action<int>)( x => { } );
52+
```
53+
54+
## Suppress a warning
55+
56+
If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
57+
58+
```csharp
59+
#pragma warning disable IDE0350
60+
// The code that's violating the rule is on this line.
61+
#pragma warning restore IDE0350
62+
```
63+
64+
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md).
65+
66+
```ini
67+
[*.{cs,vb}]
68+
dotnet_diagnostic.IDE0350.severity = none
69+
```
70+
71+
To disable all of the code-style rules, set the severity for the category `Style` to `none` in the [configuration file](../configuration-files.md).
72+
73+
```ini
74+
[*.{cs,vb}]
75+
dotnet_analyzer_diagnostic.category-Style.severity = none
76+
```
77+
78+
For more information, see [How to suppress code analysis warnings](../suppress-warnings.md).

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ The following table list all the code-style rules by ID and [options](../code-st
114114
> | [IDE0100](ide0100.md) | Remove unnecessary equality operator | |
115115
> | [IDE0110](ide0110.md) | Remove unnecessary discard | |
116116
> | [IDE0120](ide0120.md) | Simplify LINQ expression | |
117+
> | [IDE0121](ide0121.md) | Simplify LINQ type check and cast | |
117118
> | [IDE0130](ide0130.md) | Namespace does not match folder structure | [dotnet_style_namespace_match_folder](ide0130.md#dotnet_style_namespace_match_folder) |
118119
> | [IDE0140](ide0140.md) | Simplify object creation | [visual_basic_style_prefer_simplified_object_creation](ide0140.md#visual_basic_style_prefer_simplified_object_creation) |
119120
> | [IDE0150](ide0150.md) | Prefer `null` check over type check | [csharp_style_prefer_null_check_over_type_check](ide0150.md#csharp_style_prefer_null_check_over_type_check) |
@@ -143,6 +144,8 @@ The following table list all the code-style rules by ID and [options](../code-st
143144
> | [IDE0306](ide0306.md) | Use collection expression for new | [dotnet_style_prefer_collection_expression](ide0306.md#dotnet_style_prefer_collection_expression) |
144145
> | [IDE0320](ide0320.md) | Make anonymous function `static` | [csharp_prefer_static_anonymous_function](ide0320.md#csharp_prefer_static_anonymous_function) |
145146
> | [IDE0330](ide0330.md) | Prefer 'System.Threading.Lock' | [csharp_prefer_system_threading_lock](ide0330.md#csharp_prefer_system_threading_lock) |
147+
> | [IDE0340](ide0340.md) | Use unbound generic type | [csharp_style_prefer_unbound_generic_type_in_nameof](ide0340.md#csharp_style_prefer_unbound_generic_type_in_nameof) |
148+
> | [IDE0350](ide0350.md) | Use implicitly typed lambda | [csharp_style_prefer_implicitly_typed_lambda_expression](ide0350.md#csharp_style_prefer_implicitly_typed_lambda_expression) |
146149
> | [IDE1005](ide1005.md) | Use conditional delegate call | [csharp_style_conditional_delegate_call](ide1005.md#csharp_style_conditional_delegate_call) |
147150
> | [IDE1006](naming-rules.md) | Naming styles | |
148151

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ C# style rules:
141141
- [Convert `typeof` to `nameof` (IDE0082)](ide0082.md)
142142
- [Remove unnecessary equality operator (IDE0100)](ide0100.md)
143143
- [Simplify LINQ expression (IDE0120)](ide0120.md)
144+
- [Simplify LINQ type check and cast (IDE0121)](ide0121.md)
144145
- [Namespace does not match folder structure (IDE0130)](ide0130.md)
145146

146147
C# style rules:
@@ -169,6 +170,8 @@ C# style rules:
169170
- [Use collection expression for builder (IDE0304](ide0304.md)
170171
- [Use collection expression for fluent (IDE0305)](ide0305.md)
171172
- [Use collection expression for new (IDE0306)](ide0306.md)
173+
- [Use unbound generic type (IDE0340)](ide0340.md)
174+
- [Use implicitly typed lambda (IDE0350)](ide0350.md)
172175

173176
Visual Basic style rules:
174177

docs/navigate/tools-diagnostics/toc.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,6 +1617,8 @@ items:
16171617
href: ../../fundamentals/code-analysis/style-rules/ide0110.md
16181618
- name: IDE0120
16191619
href: ../../fundamentals/code-analysis/style-rules/ide0120.md
1620+
- name: IDE0121
1621+
href: ../../fundamentals/code-analysis/style-rules/ide0121.md
16201622
- name: IDE0130
16211623
href: ../../fundamentals/code-analysis/style-rules/ide0130.md
16221624
- name: IDE0140
@@ -1675,6 +1677,10 @@ items:
16751677
href: ../../fundamentals/code-analysis/style-rules/ide0320.md
16761678
- name: IDE0330
16771679
href: ../../fundamentals/code-analysis/style-rules/ide0330.md
1680+
- name: IDE0340
1681+
href: ../../fundamentals/code-analysis/style-rules/ide0340.md
1682+
- name: IDE0350
1683+
href: ../../fundamentals/code-analysis/style-rules/ide0350.md
16781684
- name: IDE1005
16791685
href: ../../fundamentals/code-analysis/style-rules/ide1005.md
16801686
- name: Miscellaneous rules

0 commit comments

Comments
 (0)