Skip to content

Latest commit

 

History

History
41 lines (30 loc) · 1.03 KB

File metadata and controls

41 lines (30 loc) · 1.03 KB

Dispose resources asynchronously.

Why is this an issue ?

Asynchronous operations free up the main thread, allowing it to handle other tasks or enter an idle state, which can lead to lower CPU usage and power consumption. Efficient resource utilization minimizes the overall environmental footprint of applications by reducing the time resources are held, lowering server load, and improving scalability.

Therefore, resources that implement IAsyncDisposable should be disposed asynchronously whenever possible.

When can it be ignored ?

This rule shouldn’t be ignored.

Non-compliant examples

public static async Task Test()
{
    using var stream = new MemoryStream(); // Non-compliant, can be disposed asynchronously
}

Compliant examples

public static async Task Test()
{
    await using var stream = new MemoryStream(); // Compliant
}
public static void Test()
{
    using var stream = new MemoryStream(); // Compliant, method is synchronous
}