What is the ValueTask equivalent of Task.Completed

2020-04-12 10:01发布

I am implementing IAsyncDisposable which requires me to return a ValueTask, but sometimes my dispose method has nothing to do. How should I return in this case?

At the moment I'm returning new ValueTask(Task.CompletedTask) which seems to work but since the point of valueTasks is to avoid creating unnecessary heap objects, I'm sure there should be a simpler and more efficient way.

1条回答
啃猪蹄的小仙女
2楼-- · 2020-04-12 10:15

All structs have a default constructor. The default constructor of ValueTask creates a completed ValueTask:

var completedValueTask = new ValueTask();

Or alternatively:

ValueTask completedValueTask = default;
查看更多
登录 后发表回答