C# 等待结果还是直接返回任务?
直接返回任务和先等待结果再返回结果之间有什么区别吗C# 等待结果还是直接返回任务?,c#,async-await,C#,Async Await,直接返回任务和先等待结果再返回结果之间有什么区别吗 public Task<Result> GetResultAsync() { return ComposeResultAsync(); // returning task directly } private Task<Result> ComposeResultAsync() { // async code } 对 public async Task<Result> GetResultAs
public Task<Result> GetResultAsync()
{
return ComposeResultAsync(); // returning task directly
}
private Task<Result> ComposeResultAsync()
{
// async code
}
对
public async Task<Result> GetResultAsync()
{
return await ComposeResultAsync(); // awaiting result first
}
private Task<Result> ComposeResultAsync()
{
// async code
}
是否有任何理由使用第一个示例,除非我需要等待另一个任务
public async Task<Result> GetResultAsync()
{
await DoSomethingElseAsync();
return await ComposeResultAsync();
}
我仍然不确定。如果一段时间后您需要一个答案,并且您想并行运行两个任务,该怎么办?最好不要等待,而是让他并行工作,在你们真正需要答案的时候等待