Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

Remove unused locals

TypeName

RemoveUnusedLocals

CheckId

CA1804

Category

Microsoft.Performance

Breaking Change

Non Breaking

A method declares a local variable but does not use the variable except possibly as the recipient of an assignment statement. For analysis by this rule, the tested assembly must be built with debugging information and the associated program database (.pdb) file must be available.

Unused local variables and unnecessary assignments increase the size of an assembly and degrade performance.

To fix a violation of this rule, remove or use the local variable. Note that the C# compiler that is included with .NET Framework 2.0 removes unused local variables when the optimize option is enabled.

Suppress a warning from this rule if the variable was compiler emitted. It is also safe to suppress a warning from this rule, or disable the rule entirely, if performance and code maintenance are not primary concerns.

The following example shows several unused local variables.

using System;
using System.Windows.Forms;

namespace PerformanceLibrary
{
   public class UnusedLocals
   {
      public void SomeMethod()
      {
         int unusedInteger;
         string unusedString = "hello";
         string[] unusedArray = Environment.GetLogicalDrives();
         Button unusedButton = new Button();
      }
   }
}
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.