Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do I return from a method that returns just Task and not Task<T>?

What do I return from D4()?

async static Task D4()
{
    Console.Write("Enter the divisor: ");
    var n = int.Parse(Console.ReadLine());
    Console.WriteLine((24 / n).ToString());

    // NONE OF THESE WORK
    // THE COMPILER COMPLAINS WITH AN ERROR THAT SAYS:
    // Since 'Program.D4()' is an async method that returns 'Task', 
    // a return keyword must not be followed by an object expression.
    // Did you intend to return 'Task<T>'?
    // return new TaskCompletionSource<object>().Task;
    // return Task.FromResult<object>(null);
    // return Task.FromResult<bool>(false);
    // return Task.FromResult(0);
}

I asked something similar earlier and it probably worked in my situation then but I have no recollection of the situation now. Also, this thread seems to suggest the same thing that I am trying. I am not sure why it doesn't work in my case.

UPDATE

For the sake of completeness, here is the whole of the relevant bit of code.

async static Task A4() { await B4(); }
async static Task B4() { await C4(); }
async static Task C4() { await D4(); }
async static Task D4()
{
    Console.Write("Enter the divisor: ");
    var n = int.Parse(Console.ReadLine());
    Console.WriteLine((24 / n).ToString());

    // return new TaskCompletionSource<object>().Task;
    // return Task.FromResult<object>(null);
    // return Task.FromResult<bool>(false);
    // return Task.FromResult(0);
}
like image 451
Water Cooler v2 Avatar asked Jun 05 '16 21:06

Water Cooler v2


3 Answers

You can think of async as constructing a Task wrapper for you. If you don't use async, then you can return an actual Task object.

Your first code example is using async but then you try to return a Task. Returning a Task isn't necessary with async:

async static Task D4()
{
  Console.Write("Enter the divisor: ");
  var n = int.Parse(Console.ReadLine());
  Console.WriteLine((24 / n).ToString());

  return; // May be removed, since it is implicit.
}

Your second code example removed the async and then tries to not return anything; in this case, since async isn't creating a Task for you, you need to create one yourself:

static Task D4()
{
  Console.Write("Enter the divisor: ");
  var n = int.Parse(Console.ReadLine());
  Console.WriteLine((24 / n).ToString());

  return Task.FromResult(0);
}

The first example (where async creates the Task for you) is more idiomatic. Note that you should also be using an await in the async method.

like image 197
Stephen Cleary Avatar answered Oct 13 '22 15:10

Stephen Cleary


Remove the async keyword so it becomes

static Task D4()
{
    return Task.FromResult(0); // or one of the others from your post
}

You don't need to await a Task.FromResult(). Even if you would use a Task that does actual work using Task.Run() as the last part of the method. See Awaiting tasks: Return task or await if no code after await

like image 39
Peter Bons Avatar answered Oct 13 '22 17:10

Peter Bons


You don't return anything from a function with Task as the return type. Similar to what you would do with void.

You could however use "return;" again as you could do with void.

Edit

You should be able to return nothing if you include "async" in the function deceleration.

Example form dotnetfiddle

like image 1
Tarek Avatar answered Oct 13 '22 17:10

Tarek