C# 6 features – Extended async / await support


facebooktwittergoogle_pluslinkedinmail

There were new keywords introduced in C# 5 – async and await. But not everyone knows that they were forbidden in catch and finally blocks. It has been fixed with C# 6 release.

Thanks to async and await keywords, developers had a chance to code in Task-based Asynchronous Pattern (TAP). The compiler did the most of the work by generating state machine that allows to handle task oriented code. The feature worked great unless it was used in catch or finally blocks. Unfortunately, it was forbidden to use await there. It turned out that this case was quite common, so developers had to put a lot of work to override this limitation.

With the release of C# 6 it was equaled and now it’s fine to use await in all: try, catch and finally blocks.

            try
            {
                await stream.WriteAsync(data, 0, data.Length);
            }
            catch (Exception ex)
            {
                await LogException(ex);
            }

 

facebooktwittergoogle_pluslinkedinmail

Leave a comment

Your email address will not be published. Required fields are marked *