Can someone please explain why the public async Task DoStuff() method below can still work without returning anything? It doesn't say void, so I assumed the return type would have to be Task.
When I remove the async and await keywords from the DoStuff() method, the compiler gives me a "not all code paths return a value" error. However, if I add the async and await keywords, it doesn't seem to need a return type, despite the lack of the void keyword in the method signature. I don't get it!
What exactly IS a Task? Microsoft explains it really poorly. Thanks.
namespace Async_and_Await_Example
{
class Program
{
static void Main(string[] args)
{
AsyncAwaitDemo demo = new AsyncAwaitDemo();
demo.DoStuff();
for (int i = 0; i < 100; i++)
{
Console.WriteLine("Working on the Main Thread...................");
}
}
}
public class AsyncAwaitDemo
{
public async Task DoStuff()
{
await Task.Run(() =>
{
CountToFifty();
});
}
private static async Task<string> CountToFifty()
{
int counter;
for (counter = 0; counter < 51; counter++)
{
Console.WriteLine("BG thread: " + counter);
}
return "Counter = " + counter;
}
}
}