Consider the following code:
private Task OnButtonClick() { //do some sync work return Task.CompletedTask; }
Many will tell you this is expensive for one of two reasons:
Neither are true.
The method is not async - it has no Async/Await
signature - and the low level code looks exactly the same as the high level code. There's no need for async state machines because there's no awaiters. You can check out the code at SharpLab.
Task.Completed
is cached as a singleton. Everyone gets the same object returned. The Task code looks like this:
private static Task s_completedTask; public static Task CompletedTask { get { var completedTask = s_completedTask; if (completedTask == null) s_completedTask = completedTask = new Task(false, (TaskCreationOptions)InternalTaskOptions.DoNotDispose, default(CancellationToken)); // benign initialization ---- return completedTask; } }