Suppress warning CS1998: This async method lacks &

2019-01-13 07:03发布

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.

13条回答
劳资没心,怎么记你
2楼-- · 2019-01-13 07:39

Another way to preserve the async keyword (in case you want to keep it) is to use:

public async Task StartAsync()
{
    await Task.Yield();
}

Once you populate the method you can simply remove the statement. I use this a lot especially when a method might await something but not every implementation actually does.

查看更多
登录 后发表回答