Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 3.0
Tools
Development Tools
FxCop
FxCop Warnings
Usage Warnings
 Review unused parameters

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Visual Studio Team System
Review unused parameters

TypeName

AvoidUnusedParameters

CheckId

CA1801

Category

Microsoft.Performance

Breaking Change

Breaking,NonBreaking

A method signature includes a parameter that is not used in the method body. The following methods are not examined by this rule:

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

Review parameters in non-virtual methods that are not used in the method body to insure no correctness 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 (i.e. the parameter should actually have been used in the method body). Exclude warnings of this rule if the parameter has to exist because of backward compatibility.

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). If the this parameter is reported, change the method declaration to include the static (Shared in Visual Basic) modifier (a breaking change).

It is safe to exclude a warning from this rule for previously shipped code for which the fix would be a breaking change.

The following shows two methods, one that violates the rule and one that satisfies the rule.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Example      David M. Kean   |   Edit   |   Show History

The following shows two methods, one that violates the rule and one that satisfies the rule.

 using System;
using System.Globalization;
  
 namespace Samples
{
public static class Foo
{
         // This method violates the rule.

public static string Bar(int first, int second)
{
return first.ToString(CultureInfo.InvariantCulture);
}
  
         // This method satisfies the rule.
         public static string Bar(int first)
{
return first.ToString(CultureInfo.InvariantCulture);
}
}
}
Tags What's this?: Add a tag
Flag as ContentBug
Does not fire on the 'this' parameter      David M. Kean   |   Edit   |   Show History
Although the documentation states that 'this' parameter can be reported as unused, this is incorrect and this violation is actually found by MarkMembersAsStatic (http://msdnwiki.microsoft.com/en-us/mtpswiki/ms245046(VS.80).aspx).
Tags What's this?: Add a tag
Flag as ContentBug
Variables that are not assigned inside the method are not taken into account      DominicZ   |   Edit   |   Show History

What about:

public static StreamWriter GetTemporaryFileForCustomer(string customerId)
{
     string tempFileName = new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName + "\\" + customerId + ".tmp";
     if(File.Exists(tempFileName))
          return File.AppendText(tempFileName,true);
     else
          return File.CreateText(tempFileName);
}

customerId is said to violate the rule, although the variable is needed to carry out the logic. What alternative could be provided to satisfy the rule?

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker