What is the ValueTask equivalent of Task.CompletedTask?

删除回忆录丶 提交于 2020-05-09 05:21:50

问题


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:


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

var completedValueTask = new ValueTask();

Or alternatively:

ValueTask completedValueTask = default;


来源:https://stackoverflow.com/questions/60899674/what-is-the-valuetask-equivalent-of-task-completedtask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!