|
| 1 | +--- |
| 2 | +title: "IDE0320: Make anonymous function static" |
| 3 | +description: "Learn about code analysis rule IDE0320: Make anonymous function static" |
| 4 | +ms.date: 11/08/2024 |
| 5 | +f1_keywords: |
| 6 | +- IDE0320 |
| 7 | +helpviewer_keywords: |
| 8 | +- IDE0320 |
| 9 | +dev_langs: |
| 10 | +- CSharp |
| 11 | +--- |
| 12 | +# Make anonymous function static (IDE0320) |
| 13 | + |
| 14 | +| Property | Value | |
| 15 | +|--------------------------|-------------------------------------------| |
| 16 | +| **Rule ID** | IDE0320 | |
| 17 | +| **Title** | Make anonymous function static | |
| 18 | +| **Category** | Style | |
| 19 | +| **Subcategory** | Language rules (modifier preferences) | |
| 20 | +| **Applicable languages** | C# | |
| 21 | +| **Options** | `csharp_prefer_static_anonymous_function` | |
| 22 | + |
| 23 | +## Overview |
| 24 | + |
| 25 | +This style rule flags [anonymous functions](../../../csharp/language-reference/operators/lambda-expressions.md) that can be marked `static`. |
| 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_prefer_static_anonymous_function |
| 32 | + |
| 33 | +| Property | Value | Description | |
| 34 | +|--------------------------|-----------------------------------------|--------------------------------------------------| |
| 35 | +| **Option name** | csharp_prefer_static_anonymous_function | | |
| 36 | +| **Option values** | `true` | Prefer anonymous functions to be marked `static` | |
| 37 | +| | `false` | Disables the rule | |
| 38 | +| **Default option value** | `true` | | |
| 39 | + |
| 40 | +## Example |
| 41 | + |
| 42 | +```csharp |
| 43 | +// Code with violations. |
| 44 | +M(x => x + 1); |
| 45 | +M(delegate (int x) { return x + 1; }); |
| 46 | + |
| 47 | +void M(Func<int, int> f) { } |
| 48 | +``` |
| 49 | + |
| 50 | +```csharp |
| 51 | +// Fixed code. |
| 52 | +M(static x => x + 1); |
| 53 | +M(static delegate (int x) { return x + 1; }); |
| 54 | + |
| 55 | +void M(Func<int, int> f) { } |
| 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 IDE0320 |
| 64 | +// The code that's violating the rule is on this line. |
| 65 | +#pragma warning restore IDE0320 |
| 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.IDE0320.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 | +- [Static anonymous functions](/dotnet/csharp/language-reference/proposals/csharp-9.0/static-anonymous-functions) |
0 commit comments