Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
| Property | Value |
|---|---|
| Rule ID | IDE0390, IDE0391 |
| Title | Make method synchronous |
| Category | Style |
| Subcategory | Unnecessary code rules (modifier preferences) |
| Applicable languages | C# |
| Options | None |
Overview
These rules flag async methods that don't contain any await expressions. For better performance, remove the async modifier and adjust the return type, because an async method that never awaits anything runs synchronously but with the overhead of the async state machine.
The two rules target different scenarios:
- IDE0390: Reports
asyncmethods, local functions, and lambda expressions that aren't interface implementations or overrides. - IDE0391: Reports
asyncmethods that implement an interface member or override a base class method.
To avoid breaking the ability of callers to await the method through the interface, use caution when applying the fix to interface implementations or overrides. Removing async can have different behavioral implications in those cases.
Example
// Code with violations.
// IDE0390: Regular method with no 'await'.
public async Task<int> GetValueAsync()
{
return 42;
}
// IDE0391: Override method with no 'await'.
public override async Task<string> GetNameAsync()
{
return "example";
}
// Fixed code.
// Remove 'async' and return the value directly.
public Task<int> GetValueAsync()
{
return Task.FromResult(42);
}
// Remove 'async' and return the value directly.
public override Task<string> GetNameAsync()
{
return Task.FromResult("example");
}
Suppress a warning
If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
#pragma warning disable IDE0390
// The code that's violating the rule is on this line.
#pragma warning restore IDE0390
To disable the rule for a file, folder, or project, set its severity to none in the configuration file.
[*.cs]
dotnet_diagnostic.IDE0390.severity = none
dotnet_diagnostic.IDE0391.severity = none
To disable all of the code-style rules, set the severity for the category Style to none in the configuration file.
[*.cs]
dotnet_analyzer_diagnostic.category-Style.severity = none
For more information, see How to suppress code analysis warnings.