The async is not really part of the inherited method signature, but rather a signal to the compiler that he needs to compile and rewrite the method according to the pattern for async methods.
That way, you can leave the async legacy methods if the legacy method does not use the await keyword.
Note that you still have to return Task or Task<T> , this part is part of the inherited method signature.
So this will give you a warning:
class Base { public virtual async Task<int> Method() { await Task.Delay(10); return 42; } } class Derived : Base {
A warning:
Warning: CS1998 There are no “wait” statements in this asynchronous method and will execute synchronously. Consider using the await operator to wait for non-blocking API calls or await Task.Run(...) to work with processor binding in the background thread.
This, however, will not result in a warning:
class Derived : Base { public override Task<int> Method() { return Task.FromResult(42); } }
Note that I changed the return in this last method because the part of the “magic” that the async keyword brings is to automatically wrap the return value inside the Task<T> . If you have other ways to get Task<T> , obviously you don't need to wrap the result, as I did above.
source share