Refactoring: using statement without scope, when does the implicit `Dispose` call happen?

Resharper suggests C# 8.0 using declaration feature:

 public async Task<Result> Handle(CancelInitiatedCashoutCommand command, 
                                  CancellationToken cancellationToken)
 {  
    using var scope = ...;
    ...
 } // <- scope will be Disposed on leaving its scope (here on Handle method's scope)

That is a C#8 using statement, and the object referenced by scope is disposed when the variable itself goes out of scope.

In this case, that would be after your Task has completed.


I was wondering the same thing. The using declaration moves out of scope at the end of the method, and is only disposed of then. Microsoft docs states the following:

the file is disposed when the closing brace for the method is reached. That's the end of the scope in which file is declared.

It would seem that if you have a using statement, it would dispose of the variable at the end of the using brace, as opposed to the using declaration that only disposes of the variable at the end of the method. If you view this in the watch or locals window, you will immediately see it move out of scope. https://dirkstrauss.com/c-sharp-8-0-using-declarations/