Export (0) Print
Expand All

CA1804: Remove unused locals







Breaking Change


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 decrease 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 to disable the rule, if performance and code maintenance are not primary concerns.

The following example shows several unused local variables.

Imports System
Imports System.Windows.Forms

Namespace PerformanceLibrary

   Public Class UnusedLocals

      Sub SomeMethod()

         Dim unusedInteger As Integer 
         Dim unusedString As String = "hello" 
         Dim unusedArray As String() = Environment.GetLogicalDrives()
         Dim unusedButton As New Button()

      End Sub 

   End Class 

End Namespace
© 2015 Microsoft