Let's say for example I have this code:
public static class MyClass
{
private static async Task<int> GetZeroAsync()
{
return await Task.FromResult(0);
}
public static int GetZero()
{
return GetZeroAsync().Result;
}
}
In GetZero()
, I am directly reading from the .Result
property of the Task that's returned from GetZeroAsync()
. Is it safe to read directly from the .Result
property, since the returned Task is already completed (i.e. will it incur any deadlocks, thread duplication, or context synchronization issues)?
The reason I'm asking is because after reading all of the answers at How to call asynchronous method from synchronous method in C#? as well as Stephen Cleary's article here https://msdn.microsoft.com/en-us/magazine/mt238404.aspx, it makes me anxious to use .Result
.