Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The awaitable and awaiter In C# 5.0 Asynchronous

Task or Task<TResult> object is awaitable, so we can use await key on those whose return value is Task or Task<TResult>. Task or Task<TResult> are the most frequently-used awaitable object.

We also can define our own awaitable object.The object should has below qualification.

  1. It has a GetAwaiter() method (instance method or extension method);
  2. Its GetAwaiter() method returns an awaiter. An object is an awaiter if:
    • It implements INotifyCompletion or ICriticalNotifyCompletion interface;
    • It has an IsCompleted, which has a getter and returns a Boolean;
    • it has a GetResult() method, which returns void, or a result.

My question is that why Microsoft didn't provide a interface to constrain these awaitable object? The current method to implement awaitable object is a little complicated.

like image 243
roast_soul Avatar asked Dec 28 '12 05:12

roast_soul


People also ask

What is an Awaiter?

(əˈweɪtə ) noun. a person who awaits something or someone.

What is an Awaitable?

An awaitable is the key construct in async code. An awaitable is a first-class object that represents a possibly asynchronous operation that may or may not have completed. We await the awaitable until the operation has completed.

What is Awaitable in C#?

The await operator suspends evaluation of the enclosing async method until the asynchronous operation represented by its operand completes. When the asynchronous operation completes, the await operator returns the result of the operation, if any.


2 Answers

It is best answered in Lucian Wischik's blog post Why must async methods return Task?

In summary (and I am not doing the blog post justice, you should read it), the issue is that Task already exists, so introducing an interface would mean

  • All the internal methods would need to be changed to the interface, a break change and thus almost impossible for the framework people to willingly do.
  • As a programmer you would constantly need to decide if you want to return Task or the interface, a decision that doesn't matter much.
  • The compiler would always need a concrete type, so even if you returned an interface from a method then it would still be compiled as Task.

The impact from the above is so massive that it doesn't make sense to provide an interface.

like image 162
Robert MacLean Avatar answered Sep 23 '22 23:09

Robert MacLean


This is in line with what they did for the foreach keyword (see section 8.8.4 of the C# language specification "The foreach statement").

Basically, it's duck-typing; if the type implements a MoveNext method and a Current property, that's all that's needed for the C# compiler to know how to iterate through a sequence exposed by an object.

This also applies with collection initializers (see section 7.6.10.3 of the C# language specification "Collection Initializers"); the only requirement is that the type implements the System.Collections.IEnumerable interface and have an Add method.

That said, the await keyword just sticks to prior precedent, not requiring specific interface implementations (although the interfaces supply those methods if you choose to use them), just a pattern of methods that the compiler can recognize.

like image 21
casperOne Avatar answered Sep 23 '22 23:09

casperOne