C
C#8mo 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
x0rld8mo ago
it's not async 🤔
Jimmacle
Jimmacle8mo ago
^ in that example you're just returning a completed task, there's nothing async there
CrosRoad95
CrosRoad958mo ago
so even if there's Task as return, it will invoked as sync function even if i do await Foo()?
Jimmacle
Jimmacle8mo ago
there's nothing to await, it just synchronously returns a completed task
CrosRoad95
CrosRoad958mo ago
so c# i can say: removes async if async is not used?
Jimmacle
Jimmacle8mo 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
Accord8mo 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.