Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between using and await using? And how can I decide which one to use?

Tags:

c#

c#-8.0

People also ask

Should I use await using?

It looks like you can only use await using with a IAsyncDisposable and you can only use using with a IDisposable since neither one inherits from the other. The only time you can use either is if the concrete class implements both and then it depends on if you are writing asynchronous code or not.

What is the difference between Sync and await?

The differences between asynchronous and synchronous include: Async is multi-thread, which means operations or programs can run in parallel. Sync is single-thread, so only one operation or program will run at a time. Async is non-blocking, which means it will send multiple requests to a server.

What exactly does await do?

The await operator suspends evaluation of the enclosing async method until the asynchronous operation represented by its operand completes. When the asynchronous operation completes, the await operator returns the result of the operation, if any.

Why We Use await in C#?

Also, we are going to see more examples, and if any third Method, as Method3 has a dependency of method1, then it will wait for the completion of Method1 with the help of await keyword. Async and await in C# are the code markers, which marks code positions from where the control should resume after a task completes.


Classic sync using

Classic using calls the Dispose() method of an object implementing the IDisposable interface.

using var disposable = new Disposable();
// Do Something...
    

Is equivalent to

IDisposable disposable = new Disposable();
try
{
    // Do Something...
}
finally
{
    disposable.Dispose();
}

New async await using

The new await using calls and await the DisposeAsync() method of an object implementing the IAsyncDisposable interface.

await using var disposable = new AsyncDisposable();
// Do Something...
    

Is equivalent to

IAsyncDisposable disposable = new AsyncDisposable();
try
{
    // Do Something...
}
finally
{
    await disposable.DisposeAsync();
}

The IAsyncDisposable Interface was added in .NET Core 3.0 and .NET Standard 2.1.

In .NET, classes that own unmanaged resources usually implement the IDisposable interface to provide a mechanism for releasing unmanaged resources synchronously. However, in some cases they need to provide an asynchronous mechanism for releasing unmanaged resources in addition to (or instead of) the synchronous one. Providing such a mechanism enables the consumer to perform resource-intensive dispose operations without blocking the main thread of a GUI application for a long time.

The IAsyncDisposable.DisposeAsync method of this interface returns a ValueTask that represents the asynchronous dispose operation. Classes that own unmanaged resources implement this method, and the consumer of these classes calls this method on an object when it is no longer needed.