Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is default(CancellationToken) equivalent to CancellationToken.None?

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?

like image 636
Cory Nelson Avatar asked Oct 23 '13 13:10

Cory Nelson


People also ask

What is default for CancellationToken?

You can also use the C# default(CancellationToken) statement to create an empty cancellation token. Two empty cancellation tokens are always equal.

What is CancellationToken CancellationToken?

A CancellationToken enables cooperative cancellation between threads, thread pool work items, or Task objects. You create a cancellation token by instantiating a CancellationTokenSource object, which manages cancellation tokens retrieved from its CancellationTokenSource.

What is CancellationToken in .NET core?

So CancellationToken can be used to terminate a request execution at the server immediately once the request is aborted or orphan. Here we are going to see some sample code snippets about implementing a CancellationToken for Entity FrameworkCore, Dapper ORM, and HttpClient calls in Asp. NetCore MVC application.

Which method can you use to cancel an ongoing operation that uses CancellationToken?

The CancellationToken is used in asynchronous task. The CancellationTokenSource token is used to signal that the Task should cancel itself. In the above case, the operation will just end when cancellation is requested via Cancel() method.


3 Answers

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.

like image 113
Cory Nelson Avatar answered Oct 21 '22 07:10

Cory Nelson


They are the same. Check source code

public static CancellationToken None
{
    get { return default(CancellationToken); }
}

From https://referencesource.microsoft.com/#mscorlib/system/threading/CancellationToken.cs,36b17ded8b1a228c

like image 21
qxg Avatar answered Oct 21 '22 08:10

qxg


CancellationToken.None simply returns new CancellationToken:

public static CancellationToken None
{    
    get
    {
        return new CancellationToken();
    }
}

Thus CancellationToken is a struct, then default(CancellationToken) will return same value. C# Spec 5.2:

For a variable of a value-type, the default value is the same as the value computed by the value-type’s default constructor

UPDATE: This behavior is not defined on MSDN, so you can rely only on current implementation.

like image 12
Sergey Berezovskiy Avatar answered Oct 21 '22 08:10

Sergey Berezovskiy