I have a Base class which provides a method with the following signature:
virtual async Task RunAsync()Derived classes should override this implementation with something like
public override async Task RunAsync()
{ await base.RunAsync(); await this.DoSomethingElse();
}Now the compiler gives me a warning that the Base.RunAsync is lacking an await statement. I want that my code is warning free, not suppressing warnings and that it runs without having to provide null checks even if the derived class doesn't provide an override implementation, therefore going with virtual Task RunAsync() (omitting the async keyword) is not possible.
For now the implementation in the base method is await Task.Delay(0); which dismisses the compiler warning, yet is somehow not "clean" in my opinion. What is the right way to cope with this subject?
1 Answer
You misunderstand how async works. It's not part of the signature, so if you don't need the await, you don't actually have to use it. So even though you are providing a Task RunAsync() method, you can just write it without the async, for example:
virtual Task RunAsync()
{ return Task.FromResult(false);
}This still allows you to make an override that does in fact use await, like so:
override async Task RunAsync()
{ await base.RunAsync(); await DoSomeOtherStuffAsync();
}This is the reason why the compiler gives you the warning - the async keyword is completely useless, and not part of the method signature. It's just a signal to the compiler that it's allowed to use the identifier await as a keyword, basically - to maintain backwards compatibility with older C# code.
EDIT:
As noted by Jeroen Mostert in the comments, from .NET Framework 4.6 on, the Task.CompletedTask property is public, so you can do this:
virtual Task RunAsync()
{ return Task.CompletedTask;
}So if you can use 4.6+, this is the newest, cleanest option. And of course, if you need the task to actually return a value, Task.FromResult is still there :)