Yes, you need to use the override keyword, otherwise the method will be hidden by the definition in the derived class.
When a method is declared as a virtual method in a base class and that method has the same definition in a derived class then there is no need to override it in the derived class.
If a method is declared async, make sure there is an await! If your code does not have an await in its body, the compiler will generate a warning but the state machine will be created nevertheless, adding unnecessary overhead for an operation that will actually never yield.
In asynchronous virtual learning the learner enriches their knowledge at their own tempo at a time convenient for themselves. Learning is not restricted by place and time. The learning process is not dependent on fixed time intervals which serve to regulate the sequence of activities.
virtual
method may be marked as async
abstract
method cannot be marked as async
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.
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.FromResult(default(object));
}
The key changes from your implementation are the following:
void
to Task
(remember async
is not actually part of the return type). Unlike returning void
, when a Task
is returned calling code has the ability to do any of the following:
async
modifier, since the method does not need to await
anything. Instead, simply return an already-completed Task
instance. Methods which override this method will still be able to use the async
modifier if they need it.Yes, it's fine, but you should use async Task
instead of async void
. I have an MSDN article that explains why.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With