When I create a default CancellationToken
I can see in the debugger that the CancellationToken
has a CancellationTokenSource
associated with it which is stored in the private m_source
field:
I am wondering how can that be as for structs the default
keyword "will return each member of the struct initialized to zero or null depending on whether they are value or reference types" and CancellationTokenSource
is a reference type.
CancellationToken
does have 2 constructors that set this field however they are irrelevant as default(CancellationToken)
doesn't call constructors and new CancellationToken()
(which has the exact same behavior) doesn't call a constructor becuase structs can't have parameterless constructors (yet).
default(CancellationToken)
does create aCancellationToken
wherem_source
isnull
. You can see that by getting the value of that private field using reflection:Output:
You can also see that by pining only the relevant field in the debugger:
So, what happens?
The debugger, in order to display the contents of the
CancellationToken
, accesses its properties one by one. When the innerCancellationTokenSource
isnull
theWaitHandle
property creates and sets a defaultCancellationTokenSource
before delegating to itsWaitHandle
property:In conclusion,
default(CancellationToken)
andnew CancellationToken
create an empty struct wherem_source
isnull
but by looking at the struct in the debugger you are filling that field with a defaultCancellationTokenSource
instance that can't be cancelled.