CA1801: Review unused parameters

TypeName ReviewUnusedParameters
CheckId CA1801
Category Microsoft.Usage
Breaking change 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.

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.

  • 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.

If you're using FxCop analyzers, 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.

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 Assert. The functions in the Assert class are static, so there's no need to access the this parameter in the method body.

Example

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

using System;
using System.Globalization;

namespace Samples
{
    public static class TestClass
    {
        // 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);
        }
    }
}

CA1811: Avoid uncalled private code

CA1812: Avoid uninstantiated internal classes

CA1804: Remove unused locals