C
C#15mo ago
CrosRoad95

❔ What happening in background when i return Task.CompletedTask?

I just wonder, what c# doing if i have async method returning Task.completedTask? does it try to synchronously continue without actualy creating a task?
public Task Foo() => Task.CompletedTask;
public Task Foo() => Task.CompletedTask;
7 Replies
x0rld
x0rld15mo ago
it's not async 🤔
Jimmacle
Jimmacle15mo ago
^ in that example you're just returning a completed task, there's nothing async there
CrosRoad95
CrosRoad95OP15mo ago
so even if there's Task as return, it will invoked as sync function even if i do await Foo()?
Jimmacle
Jimmacle15mo ago
there's nothing to await, it just synchronously returns a completed task
CrosRoad95
CrosRoad95OP15mo ago
so c# i can say: removes async if async is not used?
Jimmacle
Jimmacle15mo ago
it doesn't remove it, but you don't actually get any benefits of async you just end up with more overhead that specific method isn't async because it doesn't await anything in its body, that's just a normal method that returns a task
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?