1

Should I await or just return task? Given following example of wrapper, what are consequences of using either async/await or returning a task?

class IFoo{
   Task BarAsync();
}

class FooWrapper{
   private IFoo _foo;
   public Task BarAsync1()
   {
      Trace.WriteLine("BarAsync1 called");
      return _foo.BarAsync();
   }
   public async Task BarAsync2(){
      Trace.WriteLine("BarAsync2 called");
      await _foo.BarAsync();
   }
}

Is there a guidline for simmilar scenarios?

Liero
  • 25,216
  • 29
  • 151
  • 297
  • Very similar to [http://stackoverflow.com/q/35941724/15498](http://stackoverflow.com/q/35941724/15498) – Damien_The_Unbeliever Mar 14 '16 at 08:59
  • `BarSync1` does not have any overhead. `BarSync2` will create a slight overhead because the compiler will generate a state machine that is not really needed. There is a [Roslyn issue on Github](https://github.com/dotnet/roslyn/issues/1981) suggesting that the compiler should optimize the second case to the first case. – Martin Liversage Mar 14 '16 at 09:10

0 Answers0