Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 38 additions & 38 deletions docs/fundamentals/code-analysis/quality-rules/ca2016.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ The rule can detect a variety of violations. The following examples show cases t

### Example 1

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

```csharp
using System.Threading;
Expand All @@ -62,7 +62,7 @@ namespace ConsoleApp
{
}

public static void MyMethod(CancellationToken c)
public static void MyMethod(CancellationToken ct)
{
MyMethodWithDefault();
}
Expand All @@ -72,38 +72,38 @@ namespace ConsoleApp

Fix:

Forward the `c` parameter:
Forward the `ct` parameter:

```csharp
public static void MyMethod(CancellationToken c)
{
MyMethodWithDefault(c);
}
public static void MyMethod(CancellationToken ct)
{
MyMethodWithDefault(ct);
}
```

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

Explicitly pass `default`:

```csharp
public static void MyMethod(CancellationToken c)
{
MyMethodWithDefault(default);
}
public static void MyMethod(CancellationToken ct)
{
MyMethodWithDefault(default);
}
```

Or explicitly pass `CancellationToken.None`:

```csharp
public static void MyMethod(CancellationToken c)
{
MyMethodWithDefault(CancellationToken.None);
}
public static void MyMethod(CancellationToken ct)
{
MyMethodWithDefault(CancellationToken.None);
}
```

### Example 2

The rule will suggest forwarding the `c` parameter from `MyMethod` to the `MyMethodWithOverload` invocation, because the method has an overload that takes a `CancellationToken` parameter:
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:

```csharp
using System.Threading;
Expand All @@ -120,7 +120,7 @@ namespace ConsoleApp
{
}

public static void MyMethod(CancellationToken c)
public static void MyMethod(CancellationToken ct)
{
MyMethodWithOverload();
}
Expand All @@ -130,33 +130,33 @@ namespace ConsoleApp

Fix:

Forward the `c` parameter:
Forward the `ct` parameter:

```csharp
public static void MyMethod(CancellationToken c)
{
MyMethodWithOverload(c);
}
public static void MyMethod(CancellationToken ct)
{
MyMethodWithOverload(ct);
}
```

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

Explicitly pass `default`:

```csharp
public static void MyMethod(CancellationToken c)
{
MyMethodWithOverload(default);
}
public static void MyMethod(CancellationToken ct)
{
MyMethodWithOverload(default);
}
```

Or explicitly pass `CancellationToken.None`:

```csharp
public static void MyMethod(CancellationToken c)
{
MyMethodWithOverload(CancellationToken.None);
}
public static void MyMethod(CancellationToken ct)
{
MyMethodWithOverload(CancellationToken.None);
}
```

## Non-violation examples
Expand All @@ -174,7 +174,7 @@ namespace ConsoleApp
{
}

public static void MyMethod(CancellationToken c, int lastParameter)
public static void MyMethod(CancellationToken ct, int lastParameter)
{
MyMethodWithDefault();
}
Expand All @@ -195,7 +195,7 @@ namespace ConsoleApp
{
}

public static void MyMethod(CancellationToken c)
public static void MyMethod(CancellationToken ct)
{
MyMethodWithDefault();
}
Expand All @@ -219,7 +219,7 @@ namespace ConsoleApp
{
}

public static void MyMethod(CancellationToken c)
public static void MyMethod(CancellationToken ct)
{
MyMethodWithOverload();
}
Expand All @@ -240,7 +240,7 @@ namespace ConsoleApp
{
}

public static void MyMethod(CancellationToken c1, CancellationToken c2)
public static void MyMethod(CancellationToken ct1, CancellationToken ct2)
{
MyMethodWithDefault();
}
Expand All @@ -257,11 +257,11 @@ namespace ConsoleApp
{
public static class MyTestClass
{
public static void MyMethodWithDefault(CancellationToken c1 = default, CancellationToken c2 = default)
public static void MyMethodWithDefault(CancellationToken ct1 = default, CancellationToken ct2 = default)
{
}

public static void MyMethod(CancellationToken c)
public static void MyMethod(CancellationToken ct)
{
MyMethodWithDefault();
}
Expand All @@ -278,15 +278,15 @@ namespace ConsoleApp
{
public static class MyTestClass
{
public static void MyMethodWithOverload(CancellationToken c1, CancellationToken c2)
public static void MyMethodWithOverload(CancellationToken ct1, CancellationToken ct2)
{
}

public static void MyMethodWithOverload()
{
}

public static void MyMethod(CancellationToken c)
public static void MyMethod(CancellationToken ct)
{
MyMethodWithOverload();
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.