I've got an interface with some async functions. Some of the classes that implements the interface does not have anything to await, and some might just throw. It's a bit annoying with all the warnings.
When not using await in a async function.
Is it possible to suppress the message?
public async Task<object> test()
{
throw new NotImplementedException();
}
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.
I know this is an old thread, and perhaps this won't have the right effect for all usages, but the following is as close as I can get to being able to simply throw a NotImplementedException when I haven't yet implemented a method, without altering the method signature. If it's problematic I'd be happy to know about it, but it barely matters to me: I only use this while in development anyway, so how it performs isn't all that important. Still, I'd be happy to hear about why it's a bad idea, if it is.
Here's the type I added to make that possible.
Here is some alternatives depending on your method signature.
If you don't have anything to await then return Task.FromResult
Just as an update to Stephen's Answer, you no longer need to write the
TaskConstants
class as there is a new helper method:Try this:
[System.Diagnostics.CodeAnalysis.SuppressMessage("Await.Warning", "CS1998:Await.Warning")]
See: https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.suppressmessageattribute?view=netframework-4.7.2