Επεξεργασία

Κοινή χρήση μέσω


CA1801: Review unused parameters

Property Value
Rule ID CA1801
Title Review unused parameters
Category Usage
Fix is breaking or non-breaking Non-breaking - If the member is not visible outside the assembly, regardless of the change you make.

Non-breaking - If you change the member to use the parameter within its body.

Breaking - If you remove the parameter and it is visible outside the assembly.
Enabled by default in .NET 9 No

Cause

A method signature includes a parameter that's not used in the method body.

This rule does not examine the following kinds of methods:

  • Methods referenced by a delegate.

  • Methods used as event handlers.

  • Serialization constructors (see guidelines).

  • Serialization GetObjectData methods.

  • Methods declared with the abstract (MustOverride in Visual Basic) modifier.

  • Methods declared with the virtual (Overridable in Visual Basic) modifier.

  • Methods declared with the override (Overrides in Visual Basic) modifier.

  • Methods declared with the extern (Declare statement in Visual Basic) modifier.

This rule does not flag parameters that are named with the discard symbol, for example, _, _1, and _2. This reduces warning noise on parameters that are needed for signature requirements, for example, a method used as a delegate, a parameter with special attributes, or a parameter whose value is implicitly accessed at run time by a framework but is not referenced in code.

Note

This rule has been deprecated in favor of IDE0060. For information about how to enforce the IDE0060 analyzer at build, see code-style analysis.

Rule description

Review parameters in non-virtual methods that are not used in the method body to make sure no incorrectness exists around failure to access them. Unused parameters incur maintenance and performance costs.

Sometimes, a violation of this rule can point to an implementation bug in the method. For example, the parameter should have been used in the method body. Suppress warnings of this rule if the parameter must exist because of backward compatibility.

How to fix violations

To fix a violation of this rule, remove the unused parameter (a breaking change), or use the parameter in the method body (a non-breaking change).

When to suppress warnings

It is safe to suppress a warning from this rule:

  • In previously shipped code for which the fix would be a breaking change.

  • For the this parameter in a custom extension method for Microsoft.VisualStudio.TestTools.UnitTesting.Assert. The functions in the Assert class are static, so there's no need to access the this parameter in the method body.

Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

#pragma warning disable CA1801
// The code that's violating the rule is on this line.
#pragma warning restore CA1801

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.CA1801.severity = none

For more information, see How to suppress code analysis warnings.

Configure code to analyze

Use the following option to configure which parts of your codebase to run this rule on.

You can configure this option for just this rule, for all rules it applies to, or for all rules in this category (Performance) that it applies to. For more information, see Code quality rule configuration options.

Include specific API surfaces

You can configure which parts of your codebase to run this rule on, based on their accessibility. For example, to specify that the rule should run only against the non-public API surface, add the following key-value pair to an .editorconfig file in your project:

dotnet_code_quality.CAXXXX.api_surface = private, internal

By default, the CA1801 rule applies to all API surfaces (public, internal, and private).

Example

The following example shows two methods. One method violates the rule and the other method satisfies the rule.

// This method violates the rule.
public static string GetSomething(int first, int second)
{
    return first.ToString(CultureInfo.InvariantCulture);
}

// This method satisfies the rule.
public static string GetSomethingElse(int first)
{
    return first.ToString(CultureInfo.InvariantCulture);
}