async/await function comparison

前端 未结 3 1454
南旧
南旧 2021-01-16 11:57

I am trying to understand async/await and I am wondering if both methods work identically.If not can you explain why?

       public async Task          


        
3条回答
  •  孤城傲影
    2021-01-16 12:31

    First method: async keyword gives compiler signal for generating state machine. Event if you have empty void method marked as async the compiler will generate state machine.

    Second method: You are returning "hot task" - task that is already completed. This method will act like the normal method.

    Btw it is good idea for second scenario to cache such tasks. For example you can create dictionary witch and return cached task. If you do that you won't allocate each time new task on the heap.

提交回复
热议问题