Async OOP 1: Inheritance and Interfaces
• CommentsBefore we dive all the way into “asynchronous OOP”, let’s address one fairly common question: how does one deal with inheritance of asynchronous methods? What about an “asynchronous interface”?
Fortunately, async
does work well with inheritance (and interfaces). Remember that async
is an implementation detail, so interfaces can’t be defined with async
. To define an asynchronous method in an interface, you just need to define a method with the same signature, minus the async
keyword:
You can then implement it using async
:
If you have an implementation that isn’t async
, you can use TaskCompletionSource<T>
or one of its shorthand forms such as Task.FromResult
to implement the asynchronous method signature synchronously.
Similarly, if you have a base class method that returns Task
or Task<T>
(which may be asynchronous or synchronous), you can override it with an asynchronous or synchronous method.
Update (2014-12-01): For more details, see Recipe 10.1 in my Concurrency Cookbook.
Next time, we’ll take a look at asynchronous constructors.