Default parameter for CancellationToken

It turns out that the following works:

Task<x> DoStuff(...., CancellationToken ct = default(CancellationToken))

…or:

Task<x> DoStuff(...., CancellationToken ct = default) // C# 7.1 and later

which, according to the documentation, is interpreted the same as CancellationToken.None:

You can also use the C# default(CancellationToken) statement to create
an empty cancellation token.

Leave a Comment