Search⌘ K

Awaitable

Explore how to create and use awaitable types in C# to manage asynchronous operations. Understand the requirements for GetAwaiter, the role of INotifyCompletion, and how await controls program flow without blocking threads.

We'll cover the following...

Awaitable

In our previous example, we have used the await operator with objects of type Task. What types are allowed to be used with the await operator? The requirements are as follows:

  • The type should have a method GetAwaiter() that returns an instance of a class satisfying specific properties. Say if the return type is class X then it must meet the following bullet points.

  • X must implement the interface INotifyCompletion and optionally the ICriticalNotifyCompletion interface.

  • X must have an accessible, readable instance property IsCompleted of type bool.

  • X must have an accessible instance method GetResult() with no parameters and no type parameters.

A type which satisfies the ...