C
C#14mo 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 👻 🎃
x0rld 👻 🎃14mo ago
it's not async 🤔
Jimmacle
Jimmacle14mo ago
^ in that example you're just returning a completed task, there's nothing async there
CrosRoad95
CrosRoad95OP14mo ago
so even if there's Task as return, it will invoked as sync function even if i do await Foo()?
Jimmacle
Jimmacle14mo ago
there's nothing to await, it just synchronously returns a completed task
CrosRoad95
CrosRoad95OP14mo ago
so c# i can say: removes async if async is not used?
Jimmacle
Jimmacle14mo 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
Accord13mo 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.
Want results from more Discord servers?
Add your server