0

I have a piece of code:

...
await func();

where function "func" is defined as:

private Task func()
{
}

and this function definition gives an error:

func(): not all code paths return a value.

What kind of value it requires from me here?

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Kibernetik
  • 2,947
  • 28
  • 35

2 Answers2

4

you are missing the async keyword in your function:

private async Task func()
{
}

Without it you are defining a function that is returning an object of type Task but you have no row of return someTask; and therefore get that compilation error. So unless you add it you will have to return some task.

Otherwise, when specifying the async keyword you state that this function will be executing some code that can be awaited. If in it you do not await any Task you will receive a warning of: "This async method lacks 'await' operators and will run synchronously.". For more about the warning message read this question.

This will explain better How and When to use async and await

Community
  • 1
  • 1
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • When I define it like this, I get a warning: "This async method lacks 'await' operators and will run synchronously." – Kibernetik Oct 05 '16 at 12:01
  • @Kibernetik - Did this help you understand the issue? – Gilad Green Oct 05 '16 at 12:27
  • 1
    Thank you very much for your links Gilad Green! I will mark your answer as a solution and will study them. Seems I am lacking some basic understanding of await/async functionality... – Kibernetik Oct 05 '16 at 12:30
2

You have a return type for the method but you don't return anything.

private Task func()
{
  return new Task();//the task
}
Lidaranis
  • 765
  • 3
  • 9