Why would one use Task over ValueTask in C#?

后端 未结 4 472
情歌与酒
情歌与酒 2020-12-04 05:27

As of C# 7.0 async methods can return ValueTask. The explanation says that it should be used when we have a cached result or simulating async via synchronous code. How

4条回答
  •  甜味超标
    2020-12-04 06:04

    A more recent info from Marc (Aug 2019)

    Use Task when something is usually or always going to be genuinely asynchronous, i.e. not immediately complete; use ValueTask when something is usually or always going to be synchronous, i.e. the value will be known inline; also use ValueTask in a polymorphic scenario (virtual, interface) where you can't know the answer.

    Source: https://blog.marcgravell.com/2019/08/prefer-valuetask-to-task-always-and.html

    I followed above blog post for a recent project when I had similar questions.

提交回复
热议问题