I have a method in my base class with the following signature.
public virtual async Task<TagBuilder> RenderAsync(int columnWidth)
{
// ...
}
This works fine, but I have a lot of derived classes that override this method. And some of those overrides do not use await.
This gives me a warning.
Warning CS1998 This async method lacks ‘await’ operators and will run synchronously. Consider using the ‘await’ operator to await non-blocking API calls, or ‘await Task.Run(…)’ to do CPU-bound work on a background thread.
What is the best way to address this warning? First off, I don’t want warnings all over my code. But I need async in the base class. And I want to address this in a sanctioned way that doesn’t introduce any unnecessary overhead.
>Solution :
You can remove async from your derived classes and return a value using Task.FromResult()
Base:
public virtual async Task<TagBuilder> RenderAsync(int columnWidth)
{
// ...
}
Derived:
public override Task<TagBuilder> RenderAsync(int columnWidth)
{
TagBuilder tag = GetTag();
return Task.FromResult(tag);
}