Consider the following code snippet in which the MyMethod and its asynchronous version MyMethodAsync is called in different ways:
using System;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static int MyMethod()
{
System.Threading.Thread.Sleep(1000);
return 42;
}
static async Task<int> MyMethodAsync()
{
await Task.Delay(1000);
return 42;
}
static void Main(string[] args)
{
var result1 = MyMethod(); // int
var result2 = MyMethodAsync(); // Task<int>
var result3 = Task.Run(() => MyMethod()); // Task<int>
var result4 = Task.Run(() => MyMethodAsync()); // Task<int>
}
}
}
In each case, I have commented the return type.
The question is why the type of result4 is Task<int> too? Shouldn't it be Task<Task<int>>?
BTW, is there any case that calling an async method by Task.Run could be beneficial?