Review unused parameters
Review unused parameters
Microsoft docsDescription
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.
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 System.Runtime.Serialization.ISerializable.GetObjectData methods.
- Methods declared with the
abstract(MustOverridein Visual Basic) modifier.
- Methods declared with the
virtual(Overridablein Visual Basic) modifier.
- Methods declared with the
override(Overridesin Visual Basic) modifier.
- Methods declared with the
extern(Declarestatement 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 runtime by a framework but is not referenced in code. This rule has been deprecated in favor of IDE0060. For information about how to enforce the IDE0060 analyzer at build, see code-style analysis.
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).
Example
#pragma warning disable CA1801
// The code that's violating the rule is on this line.
#pragma warning restore CA1801When to suppress
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
thisparameter in a custom extension method for Microsoft.VisualStudio.TestTools.UnitTesting.Assert. The functions in the Microsoft.VisualStudio.TestTools.UnitTesting.Assert class are static, so there's no need to access thethisparameter in the method body.