If an `OperationCanceledException` is thrown because a `CancellationToken` was signaled, what state is the `Task` in?

.NET interview question for Advanced practice.

Answer

The task is in the Canceled state.

Explanation

When an async operation is cancelled via a CancellationToken and throws an OperationCanceledException, the associated Task transitions to the Canceled state. This is distinct from the Faulted state, which is used for other, unexpected exceptions. This allows callers to differentiate between a successful cancellation and a genuine failure.

Related Questions