61

This blog post mentions the new Task APIs, including a new Task.CompletedTask property introduced in .NET 4.6.

Why was this added? How is this better than, say, Task.FromResult(whatever)?

fiat
  • 15,501
  • 9
  • 81
  • 103
Gigi
  • 28,163
  • 29
  • 106
  • 188
  • 4
    Isn't clearer intent enough? – svick May 27 '15 at 21:02
  • 2
    The reason is that you can spare memory by creating static immutable tasks which are in fixed states. If you need to return a completed task because e.g. you only optionally download stuff from the net and otherwise return a dummy task which is set to completed so the caller which waits on your task can continue right away. Until now you needed to create your own instance and cache it or create it every time again via TaskCompletionSource. – Alois Kraus May 27 '15 at 21:05
  • 3
    The blog post already explains why `Task.CompletedTask` was added... It specifically mentions "library code that cares about performance and avoiding allocations". Now compare that with your `Task.FromResult(whatever)`: does that avoid allocations? No, it doesn't. –  May 27 '15 at 21:19

3 Answers3

60

Task.FromResult(whatever) works for Task<TResult>, but until 4.6 there was nothing for the nongeneric task. You could use FromResult with a dummy value and implicitly cast it to Task, but that somewhat obfuscates the intent (you're not really returning any asynchronous value) and allocates objects underneath (whereas CompletedTask can be cached and shared between all callers).

It's not uncommon to see custom static completed tasks in current (4.5.2 and older) codebases, so in my opinion it makes sense to incorporate it to the framework itself.

Honza Brestan
  • 10,637
  • 2
  • 32
  • 43
27

Task.CompletedTask property is important when you need to give a caller a dummy Task (that doesn't return a value/result) that's already completed. This might be necessary to fulfill an "interface" contract or testing purposes.

Task.FromResult(data) also returns a dummy Task, but this time with data or a result. You probably would be doing this because you already have the data and have no need to perform any operation to get it.

Example - Task.CompletedTask

public Task DoSomethingAsync()
{
    return Task.CompletedTask; // null would throw exception on await
}

Example - Task.FromResult(data)

public Task<User> GetUserAsync()
{
    if(cachedUser != null) 
    {
        return Task.FromResult(cachedUser);
    }
    else
    {
        return GetUserFromDb();
    }
}
Orson
  • 14,981
  • 11
  • 56
  • 70
-3

If you want to await a function in a class, use task function then return Task.CompletedTask. async is not required in the function definition. otherwise, you do strange code for async like await task.yield.

public class TaskCustom
    {
        public Task Function1Async(ITestOutputHelper output)
        {
            for (int i = 0; i < 10; i++)
            {
                output.WriteLine("reached fnc1 "+i.ToString());
            }
            return Task.CompletedTask; // null would throw exception on await
        }
        public Task Function2Async(ITestOutputHelper output)
        {
            for (int i = 0; i < 10; i++)
            {
                output.WriteLine("reached fnc2 " + i.ToString());
            }
            return Task.CompletedTask; // null would throw exception on await
        }

    }


 public async Task TaskTestCompletedTask()
        {
            TaskCustom obj = new TaskCustom();
            await obj.Function1Async(output);
            await obj.Function2Async(output);
            Assert.True(true);
        }
Golden Lion
  • 3,840
  • 2
  • 26
  • 35