CancellationToken parameters must come last
CancellationToken parameters must come last
Microsoft docsDescription
Methods that perform long running operations or asynchronous operations and are cancelable normally take a cancellation token parameter. Each cancellation token has a System.Threading.CancellationTokenSource that creates the token and uses it for cancelable computations. It is common practice to have a long chain of method calls that pass around the cancellation token from the callers to the callees. Hence, a large number of methods that take part in a cancelable computation end up having a cancellation token parameter. However, the cancellation token itself is not usually relevant to the core functionality of a majority of these methods. It's considered a good API design practice to have such parameters be the last parameter in the list.
Cause
A method has a System.Threading.CancellationToken parameter that is not the last parameter.
By default, this rule analyzes the entire codebase, but this is configurable.
How to fix violations
Change the method signature to move the cancellation token parameter to the end of the list. For example, the following two code snippets show a violation of the rule and how to fix it:
Example
// Violates CA1068
public void LongRunningOperation(CancellationToken token, string usefulParameter)
{
...
}When to suppress
If the method is an externally visible public API that is already part of a shipped library, then it is safe to suppress a warning from this rule to avoid a breaking change for the library consumers.