Home > front end >  TaskTupleAwaiter vs multiple await statements?
TaskTupleAwaiter vs multiple await statements?

Time:08-02

TaskTupleAwaiter allows us to do

// For example
// Task<int> t1() { return Task.FromResult(1); }
// Task<string> t2() { return Task.FromResult("a"); }
var (r1, r2) = await (t1(), t2());

How does it compare to the following code? (Besides more concise code)

var t1task = t1();
var t2task = t2();
var r1 = await t1task;
var r2 = await t2task;

The project hasn't had any update for a while. I'm wondering if I need to remove it from our repo.

CodePudding user response:

Based on the source code await (t1(), t2()) should be the same as:

var t1task = t1();
var t2task = t2();
await Task.WhenAll(t1task, t2task);  
var r1 = t1task.Result;
var r2 = t2task.Result;

Which should be preferable compared to the code in the question.

The project hasn't had any update for a while.

The source code was updated only 5 month ago, project targets latest released ATM version of .NET - .NET 6. I would say that for smaller projects which does not have a lot of features should be just fine, so no reason to switch.

  •  Tags:  
  • c#
  • Related