All rules
CA2016Reliability Enabled by default: As suggestion

Forward the CancellationToken parameter to methods that take one

Forward the CancellationToken parameter to methods that take one

Microsoft docs

Description

This rule analyzes method definitions that take a CancellationToken as their last parameter, then analyzes all methods invoked in its body. If any of the method invocations can either accept a CancellationToken as the last parameter, or have an overload that takes a CancellationToken as the last parameter, then the rule suggests using that option instead to ensure that the cancellation notification gets propagated to all operations that can listen to it. Rule CA2016 is available in all .NET versions where the CancellationToken type is available. For the applicable versions, see the CancellationToken "Applies to" section.

Cause

This rule locates method invocations that could accept a System.Threading.CancellationToken parameter, but are not passing any, and suggests to forward the parent method's CancellationToken to them.

How to fix violations

You can either fix violations manually, or use the code fix available in Visual Studio. Hover the light bulb that appears next to the method invocation and select the suggested change.

The following example shows two suggested changes:

!Rule CA2016 - Forward the CancellationToken parameter to methods that take one

It's safe to suppress a violation of this rule if you're not concerned about forwarding the canceled operation notification to lower method invocations. You can also explicitly pass default in C# (Nothing in Visual Basic) or System.Threading.CancellationToken.None to suppress the rule violation.

The rule can detect a variety of violations. The following examples show cases that the rule can detect:

### Example 1

The rule will suggest forwarding the ct parameter from MyMethod to the MyMethodWithDefault invocation, because the method defines an optional token parameter:

Fix:

Forward the ct parameter:

If you are not concerned about forwarding cancellation notifications to lower invocations, you can either:

Explicitly pass default:

Or explicitly pass CancellationToken.None:

### Example 2

The rule will suggest forwarding the ct parameter from MyMethod to the MyMethodWithOverload invocation, because the method has an overload that takes a CancellationToken parameter:

Fix:

Forward the ct parameter:

If you are not concerned about forwarding cancellation notifications to lower invocations, you can either:

Explicitly pass default:

Or explicitly pass CancellationToken.None:

Example

using System.Threading;

namespace ConsoleApp
{
    public static class MyTestClass
    {
        public static void MyMethodWithDefault(CancellationToken ct = default)
        {
        }

        public static void MyMethod(CancellationToken ct)
        {
            MyMethodWithDefault();
        }
    }
}
Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0