Task<T>.Result returns the value stored in a task. Reading it before completion synchronously blocks the current thread; reading it after completion returns immediately. A canceled or faulted task makes Result throw, so this property adapts asynchronous APIs to synchronous code at a significant cost. section 3.2.3 section 3.2.4
Use Result with these constraints:
- Prefer reading Result only after IsCompleted is true or from a completion continuation; then the access is nonblocking. section 3.2.4 section 3.7
- Avoid using Result as a general replacement for await: blocking is inefficient, removes asynchronous benefits, and can deadlock in some environments. section 3.2.3 section 3.7
- Expect an AggregateException for faulted tasks and account separately for cancellation. section 3.2.4
Adapting an asynchronous file read
Start File.ReadAllBytesAsync("example.bin"), then read its Result property when synchronous code must obtain the byte array. This blocks until the read completes and propagates cancellation or failure. section 3.2.3
var readCompleted = File.ReadAllBytesAsync("example.bin");
var bytes = readCompleted.Result; #1
// do something with bytes
Shows the minimal synchronous bridge from File.ReadAllBytesAsync to the completed byte array. section 3.2.3
