1

I would like to know if there is a difference between these two async patterns. Obviously they will work. I would like to know if there is a hidden trap or performance overhead. What will happen with aggregateexception call stack in both cases?

    //------Pattern1-------- PassThruMethod is awaiting
    public async void EventHandler()
    {
        await PassThruMethod();
    }

    public async Task<int> PassThruMethod()
    {
        return await MyAsyncMethod();
    }

    public Task<int> MyAsyncMethod()
    {
        return Task.Run(() => 1);
    }


    //---Pattern2----- PassThruMethod is not awaiting
    public async void EventHandler()
    {
        await PassThruMethod();
    }

    public Task<int> PassThruMethod()
    {
        return MyAsyncMethod();
    }

    public Task<int> MyAsyncMethod()
    {
        return Task.Run(() => 1);
    }
ekalchev
  • 762
  • 7
  • 24

1 Answers1

2

There's no need to use async if you don't use await - since your PassThruMethod doesn't need await, don't use it. You can change it at any time if you eventually find out it's not good enough.

Using await does have some overhead (not exactly huge, but there is some), so for a case like this, there's no reason to use it. Returning Task is just fine.

Luaan
  • 62,244
  • 7
  • 97
  • 116