Short answer
- A
virtualmethod may be marked asasync - An
abstractmethod cannot be marked asasync
The reason for this is async is not actually part of the method signature. It simply tells the compiler how to handle the compilation of the method body itself (and does not apply to overriding methods). Since an abstract method does not have a method body, it does not make sense to apply the async modifier.
Long answer
Rather than your current signature in the base class, I would recommend the following if the base class provides a default implementation of the method but does not need to do any work.
protected virtual Task LoadDataAsync() {
return Task.CompletedTask;
}
The key changes from your implementation are the following:
- Change the return value from
voidtoTask(rememberasyncis not actually part of the return type). Unlike returningvoid, when aTaskis returned calling code has the ability to do any of the following:
- Wait for the operation to complete
- Check the status of the task (completed, canceled, faulted)
- Avoid using the
asyncmodifier, since the method does not need toawaitanything. Instead, simply return an already-completedTaskinstance. Methods which override this method will still be able to use theasyncmodifier if they need it.