Unable to declare Interface " async Task<myObject> MyMethod(Object myObj); "

goodguys_activate picture goodguys_activate · Oct 24, 2012 · Viewed 35.3k times · Source

I'm unable to declare

interface IMyInterface
{
   async Task<myObject> MyMethod(Object myObj);
}

The compiler tells me:

  • The modifier async isn't valid for this item
  • The async modifier can only be used for methods that have a body

Is this something that should be implemented, or does the nature of async & await prohibit this from ever occurring?

Answer

stuartd picture stuartd · Oct 24, 2012

Whether a method is implemented using async/await or not is an implementation detail. How the method should behave is a contract detail, which should be specified in the normal way.

Note that if you make the method return a Task or a Task<T>, it's more obvious that it's meant to be asynchronous, and will probably be hard to implement without being asynchronous.

From https://stackoverflow.com/a/6274601/43846