Brief explanation of Async/Await in .Net 4.5

前端 未结 3 761
我在风中等你
我在风中等你 2020-11-29 03:07

How does Asynchronous tasks (Async/Await) work in .Net 4.5?

Some sample code:

private async Task TestFunction()
{
  var x = await DoesSom         


        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-29 03:47

    It executes after the first await returns. If this thing confuses you, try to play around with breakpoints - they are fully supported by the new async pattern.

    Imagine it would look like this:

    var x = await GetSomeObjectInstance();
    var y = await GetSomeObjectInstance2(x);
    

    There probably would occur a NullReferenceException somewhere, so the first await has to return first. Otherwise, x would be null/undefined or whatever.

提交回复
热议问题