Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Addressing 'This async method lacks 'await' operators' warning in overriding method

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading