using var notNeeded = await foo();
foo
returns an object which implements IDisposable but that object can be discarded immediately. What is the idomatic way to write this line?
using var notNeeded = await foo(); // (1) possible lint error: unused variable
using var _ = await foo(); // (2) looks clean but lives until the end of the function (which is rarely an issue)
using _ = await foo(); // (3) not legal
using (await foo()); // (4) possible lint error: possible mistaken empty statement
using (await foo()) {} // (5) looks like a mistake but actually does call Dispose() immediately
Are there any more elegant ways?
My preference for disposing immediately a non-needed IDisposable
object:
(await foo()).Dispose();
...and in case it's IAsyncDisposable
:
await (await foo()).DisposeAsync();