To implement a custom awaitable object in C#, you need to create a type that can be used with the
await keyword. This requires understanding how the C# compiler translates
await into a series of method calls.
High-Level Summary
To be awaitable, your custom type must:
Have a method called GetAwaiter().
That method returns an awaiter object which must:
Implement INotifyCompletion or ICriticalNotifyCompletion
Have a bool IsCompleted { get; } property
Have a void OnCompleted(Action) method
Have a T GetResult() method (returns result after awaiting)
Components of a Custom Awaitable
1. Awaitable Object
This is the object you call await on.
2. Awaiter Object
Returned by GetAwaiter(); it contains logic for managing the async wait.
Example: Simple Custom Awaitable
Let’s create a custom awaitable that just delays and returns a message.
Custom Awaitable
public class MyCustomAwaitable
{
private readonly int _delay;
public MyCustomAwaitable(int delay)
{
_delay = delay;
}
public MyCustomAwaiter GetAwaiter()
{
return new MyCustomAwaiter(_delay);
}
}
Custom Awaiter
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
public class MyCustomAwaiter : INotifyCompletion
{
private readonly int _delay;
private Task _task;
public MyCustomAwaiter(int delay)
{
_delay = delay;
_task = Task.Delay(delay); // simulate async operation
}
public bool IsCompleted => _task.IsCompleted;
public void OnCompleted(Action continuation)
{
_task.ContinueWith(t => continuation());
}
public string GetResult()
{
// Can also throw exceptions or return results
return $"Completed after {_delay}ms!";
}
}
Usage
public async Task UseCustomAwaitable()
{
var result = await new MyCustomAwaitable(1000);
Console.WriteLine(result); // Output: Completed after 1000ms!
}
Key Concepts
Element
Purpose
GetAwaiter()
Returns the awaiter
IsCompleted
Indicates if the task is already done
OnCompleted()
Registers the continuation (what to do after await finishes)
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
To implement a custom awaitable object in C#, you need to create a type that can be used with the
awaitkeyword. This requires understanding how the C# compiler translatesawaitinto a series of method calls.High-Level Summary
To be awaitable, your custom type must:
GetAwaiter().INotifyCompletionorICriticalNotifyCompletionbool IsCompleted { get; }propertyvoid OnCompleted(Action)methodT GetResult()method (returns result after awaiting)Components of a Custom Awaitable
1. Awaitable Object
This is the object you call
awaiton.2. Awaiter Object
Returned by
GetAwaiter(); it contains logic for managing the async wait.Example: Simple Custom Awaitable
Let’s create a custom awaitable that just delays and returns a message.
Custom Awaitable
Custom Awaiter
Usage
Key Concepts
GetAwaiter()IsCompletedOnCompleted()GetResult()Real-World Use Cases
Let me know if you want:
T)CancellationToken