Is default(CancellationToken) equivalent to CancellationToken.None?

Cory Nelson picture Cory Nelson · Oct 23, 2013 · Viewed 11.6k times · Source

Looking at the implementation of CancellationToken.None, it is simply returning default(CancellationToken). However, I see no reference in CancellationToken's documentation that the two are equivalent.

I'd like to offer an API like this but not until I'm sure it'll always work:

Task DoSomething(CancellationToken token = default(CancellationToken))

Is it defined behavior that default(CancellationToken) is the same as CancellationToken.None, or is this just an implementation detail?

Answer

Cory Nelson picture Cory Nelson · Sep 17, 2015

After filing an issue with corefx, the documentation remarks have been updated to make this a guaranteed feature:

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