The following code came up in review (simplified for the sake of this example):
Code sample 1
public class Chain
{
public async Task<string> UI()
{
return await MidTier();
}
private Task<string> MidTier()
{
return Database();
}
private async Task<string> Database()
{
string result = null;
await Task.Run(() => result = "Some data");
return result;
}
}
Clearly async/await has just been missed from the MidTier method but the code compiles (and runs) correctly. I suppose it is reasonable that the compiler has inferred that the calling code and the called code are async/await operations. This assumption can be tested to a degree as intellisense seems to know that the method and called code are awaitable.
However, if async/await are added, different code is produced under the hood:
Code sample 2
public class Chain
{
public async Task<string> UI()
{
return await MidTier();
}
private async Task<string> MidTier()
{
return await Database();
}
private async Task<string> Database()
{
string result = null;
await Task.Run(() => result = "Some data");
return result;
}
}
The IL produced is different. What is going on here?