【发布时间】:2016-10-02 17:47:32
【问题描述】:
我有一个带有签名的简单异步方法:
public async Task<bool> AllowAccessAsync(string authenticatedUserToken)
调用此方法时,将其结果分配给局部变量时,我似乎有两个选择:
bool response = null;
// option 1
await this.transportService.AllowAccessAsync(authenticatedUserToken).ContinueWith(task => response = task.Result);
// option 2
response = await this.transportService.AllowAccessAsync(authenticatedUserToken);
第一个使用延续委托分配给局部变量,第二个将结果直接分配给变量。
这些结果是否相同?这两种方法有什么优势吗?有没有更好的方法来做到这一点?
【问题讨论】:
标签: c# .net asynchronous async-await task-parallel-library