The difference is a stacktrace in case of errors. If you modify the code a bit as following, you would see how results differ.
internal class Example1
{
public static void Main(string[] args)
{
Func<Task<int>> getter = async () => await Get();
int x = getter().Result;
Console.WriteLine("hello : " + x);
}
static async Task<int> Get()
{
throw new Exception("test");
await Task.Delay(1000);
return 1;
}
}
internal class Example2
{
public static void Main(string[] args)
{
Func<Task<int>> getter = () => Get();
int x = getter().Result;
Console.WriteLine("hello : " + x);
}
static async Task<int> Get()
{
throw new Exception("test");
await Task.Delay(1000);
return 1;
}
}
When Example1 throws the exception, you see a stacktrace as following. Looking at the stacktrace, you know that the Get() method was called from the anonymous method.
This exception was originally thrown at this call stack:
StackOverflow.CSharp.Example1.Get() in Example.cs
StackOverflow.CSharp.Example1.Main.AnonymousMethod__0_0() in Example.cs
However, when Example2 throws the exception, the stacktrace is reduced and does not show where Get() was called from. It could be hard to trace a potential problem.
This exception was originally thrown at this call stack:
StackOverflow.CSharp.Example2.Get() in Example.cs
In our projects, we prefer the first approach with async/await.