Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of .NET 4.6's Task.CompletedTask?

Tags:

c#

task

.net-4.6

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)?

like image 659
Gigi Avatar asked May 27 '15 20:05

Gigi


People also ask

What is Task CompletedTask for?

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.

How do I return a NULL from a task?

We can return null from a method that returns a Task because Task is a reference type. In our previous example, we return null from NonAsyncFoo() . But, awaiting null isn't legal, so await NonAsyncFoo() throws a NullReferenceException .

How do I return a task object in C#?

The recommended return type of an asynchronous method in C# is Task. You should return Task<T> if you would like to write an asynchronous method that returns a value. If you would like to write an event handler, you can return void instead. Until C# 7.0 an asynchronous method could return Task, Task<T>, or void.


2 Answers

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.

like image 151
Honza Brestan Avatar answered Sep 21 '22 12:09

Honza Brestan


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();     } } 
like image 43
Orson Avatar answered Sep 17 '22 12:09

Orson