public static async Task Test()
{
using var stream = new MemoryStream(); // Non-compliant, can be disposed asynchronously
}
Dispose resources asynchronously.
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.
This rule shouldn’t be ignored.
public static async Task Test()
{
using var stream = new MemoryStream(); // Non-compliant, can be disposed asynchronously
}
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
}