Take for example the following interface:
interface IOracle { Task<string> GetAnswerAsync(string question); }
Some implementations of this interface may use async / await . Others may not need. For example, consider this simple toy.
class SimpleOracle { public Dictionary<string, string> Lookup { get; set; }
A warning about the CS1998 compiler, of course, makes sense. The usual suggestion is to remove the async and use Task.FromResult , but it does not correspond to the subtle problem. What if the code throws an exception? Then this code conversion changes the behavior of the method: the async version completes any exception in the Task ; the non- async version will not async without try - catch explication.
Leaving the async keyword works exactly the way I want, but it raises a compiler warning, and I don't think suppressing them is reasonable.
How can I reorganize my method implementation so as not to cause a compiler warning, as well as wrapping all exceptions with Task , like any other async method?
source share