0

If I have a class that inherits a base class, and both classes inherit this interface

public interface BaseClass
{
    Task<object> GetAsync();
}

If the base class implements its method as:

public async Task<object> GetAsync()
{
    object o = async DoSomethingAsync();
    return o;
}

And the class that inherits the base class overrides the method but calls the base class method still, does it matter if you put async await? e.g?

// Option 1
public async Task<object> GetAsync()
{
    DoSomethingElse();
    return await base.GetAsync();
}

// Option 2
public Task<object> GetAsync()
{
    DoSomethingElse();
    return base.GetAsync();
}

What is the difference between the two? Is there any?

chris31389
  • 8,414
  • 7
  • 55
  • 66

2 Answers2

-1

Calling base class method is no different than calling method from different class in this regard, you should still add 'await'. Without it your derived method will be executed synchronously.

borkovski
  • 938
  • 8
  • 12
  • The existence or absence of `await` has *nothing* to do with whether the operation will be performed synchronously or asynchronously. – Servy Mar 30 '17 at 14:01
-1

Despite it appearing to be part of the signature, async is in fact an implementation detail of the method that it decorates1. There is no need to decorate a method with async just because it's overriding a method that is implemented using it.

Option 2 is fine.

There's a github issue relating to automatically re-writing Option 1 to be Option 2, but I'm not sure if/when that will arrive, since there are some arguments against it (notably in the face of exceptions)


1As further evidence that it's not part of the signature, you're not even allowed to use async when defining interface members.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • `As further evidence that it's not part of the signature, you're not even allowed to use async when defining interface members.` Considering that *the question demonstrates this* why are you repeating it in an answer? – Servy Mar 30 '17 at 13:55
  • @Servy - I don't see that the question *highlighted* its absence from the interface. The OP may not have even *noticed* its absence there. I mentioned it mostly because I've encountered a number of people who insist that `async` *is* part of the signature. – Damien_The_Unbeliever Mar 30 '17 at 13:58
  • The question mentions and interface, and is comparing the difference between implementing it with an `async` method or a non-`async` method. How could they possibly even ask that if they were convinced that it's not possible to do that? – Servy Mar 30 '17 at 13:59
  • @Servy - no, they're talking about an *inheritance* situation and were asking whether the *inheriting* class also had to use `async` because the base class had. If you'd care to read. They've already implemented the interface method in the base class, as is obvious because they're *calling* it from options 1 & 2. Which is also why I don't think the duplicate is particularly apt. – Damien_The_Unbeliever Mar 30 '17 at 14:01
  • That a base class is also involved doesn't change the point. Clearly if the two methods were of different signatures they couldn't both override the same method. But anyway, the two methods *are* both implementing the same interface *as well*, so the existence of the base class is merely an *Additional* point demonstrating that the OP already understands that the signatures aren't different, not something that contradicts it. In addition to repeating information stated in the question, you don't' actually *answer* the question (how are the two implementations different). – Servy Mar 30 '17 at 14:04